Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Disable collision between a character and a platform while keeping that platform

Discussion in '2D' started by ShadowNinja1246, Apr 8, 2023.

  1. ShadowNinja1246

    ShadowNinja1246

    Joined:
    Mar 10, 2023
    Posts:
    2
    I'm trying to make a game where you control two characters, both of which can only collide with the "universal" ground and their respective color. (For example, the green character can only collide with green ground and white ground while the red character can only collide with red ground and white ground.) I'm currently trying to disable green's collision with red ground while still keeping the red ground's collider enabled. I've got something that works, but there's a split second delay between touching the red platform and being able to pass through it, so green hesitates a bit before passing through it. If anyone could help me figure out how to remove that delay, that would be great. Attached is my player movement script and the script that disables collision for red blocks.

    Also, there's a bug where even though collision is disabled for the red platform, it still allows you to jump while touching its hitbox. Help fixing this would be appreciated.

    Player Movement:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     private float horizontal;
    8.     public float speed = 8f;
    9.     public float jumpingPower = 16f;
    10.     private bool isFacingRight = true;
    11.  
    12.     private float coyoteTime = 0.2f;
    13.     private float coyoteTimeCounter;
    14.  
    15.     private float jumpBufferTime = 0.2f;
    16.     private float jumpBufferCounter;
    17.  
    18.     [SerializeField] private Rigidbody2D rb;
    19.     [SerializeField] private Transform groundCheck;
    20.     [SerializeField] private LayerMask groundLayer;
    21.  
    22.     // Start is called before the first frame update
    23.     void Start()
    24.     {
    25.  
    26.     }
    27.  
    28.     // Update is called once per frame
    29.     void Update()
    30.     {
    31.         horizontal = Input.GetAxisRaw("Horizontal");
    32.  
    33.         if (IsGrounded())
    34.         {
    35.             coyoteTimeCounter = coyoteTime;
    36.         }
    37.         else
    38.         {
    39.             coyoteTimeCounter -= Time.deltaTime;
    40.         }
    41.  
    42.         if (Input.GetButtonDown("Jump"))
    43.         {
    44.             jumpBufferCounter = jumpBufferTime;
    45.         }
    46.         else
    47.         {
    48.             jumpBufferCounter -= Time.deltaTime;
    49.         }
    50.  
    51.         if (jumpBufferCounter > 0f && coyoteTimeCounter > 0f)
    52.         {
    53.             rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
    54.  
    55.             jumpBufferCounter = 0f;
    56.         }
    57.  
    58.         if (Input.GetButtonUp("Jump") && rb.velocity.y > 0f)
    59.         {
    60.             rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
    61.  
    62.             coyoteTimeCounter = 0f;
    63.         }
    64.  
    65.         Flip();
    66.     }
    67.  
    68.     private bool IsGrounded()
    69.     {
    70.         return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer);
    71.     }
    72.  
    73.     private void FixedUpdate()
    74.     {
    75.         rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
    76.     }
    77.  
    78.     private void Flip()
    79.     {
    80.         if (isFacingRight && horizontal < 0f || !isFacingRight && horizontal > 0f)
    81.         {
    82.             isFacingRight = !isFacingRight;
    83.             Vector3 localScale = transform.localScale;
    84.             localScale.x *= -1f;
    85.             transform.localScale = localScale;
    86.         }
    87.     }
    88.  
    89. }
    Phase Through Red:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PhaseThroughRed : MonoBehaviour
    6. {
    7.     private GameObject currentRedPlatform;
    8.  
    9.     [SerializeField] private BoxCollider2D playerCollider;
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.        
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         if (currentRedPlatform != null)
    20.         {
    21.             DisableCollision();
    22.         }
    23.     }
    24.  
    25.     private void OnCollisionEnter2D(Collision2D collision)
    26.     {
    27.         if (collision.gameObject.CompareTag("Red"))
    28.         {
    29.             currentRedPlatform = collision.gameObject;
    30.         }
    31.     }
    32.  
    33.     private void OnCollisionExit2D(Collision2D collision)
    34.     {
    35.         if (collision.gameObject.CompareTag("Red"))
    36.         {
    37.             currentRedPlatform = null;
    38.         }
    39.     }
    40.  
    41.     private void DisableCollision()
    42.     {
    43.         BoxCollider2D redCollider = currentRedPlatform.GetComponent<BoxCollider2D>();
    44.  
    45.         Physics2D.IgnoreCollision(playerCollider, redCollider);
    46.     }
    47. }
    48.  
     
    Last edited: Apr 8, 2023
  2. ShadowNinja1246

    ShadowNinja1246

    Joined:
    Mar 10, 2023
    Posts:
    2

    Attached Files: