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. Dismiss Notice

Disable collision in OnCollisionEnter without changing velocity of the colliding rigidbody

Discussion in '2D' started by Xientra, Apr 5, 2021.

  1. Xientra

    Xientra

    Joined:
    Apr 1, 2017
    Posts:
    2
    I have my player that collides with a platform and if the player is below that platform I want to disable the platform and let the player get on top.

    Simple, if I ignore how to enable the collider again for now:

    Code (CSharp):
    1.     private void OnCollisionEnter2D(Collision2D collision)
    2.     {
    3.         if (collision.collider.CompareTag("Platform"))
    4.         {
    5.             float platformTop = collision.collider.bounds.center.y + collision.collider.bounds.extents.y;
    6.             float playerBottom = col.bounds.center.y + col.bounds.extents.y;
    7.  
    8.             if (platformTop > playerBottom)
    9.                 Physics2D.IgnoreCollision(collision.collider, col, true);
    10.         }
    11.     }

    Now if I jump with the player against the platform from below it disables it just like I want it to.
    The Problem is that when that happens the velocity of the rigidbody on my player is still changed to 0 vertically because it hit a ceiling.

    I've tried disabling the collision with:
    Code (CSharp):
    1.     collision.collider.enabled = false;
    2.     collision.collider.isTrigger = true;
    but the same problem still occurs.


    Is there any way to fix this? Can i execute code before the velocity is changed? Or can I prevent a change of velocity in this frame?
    Or do I simply have to work around this?


    Thank you for any answers :D
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,533
    OnCollisionEnter2D isn't tellnig you it's about to collide, it's telling you it has collided and solved after the fact. There's no callback that tells you it's about to collide no.
     
    Xientra likes this.
  3. Xientra

    Xientra

    Joined:
    Apr 1, 2017
    Posts:
    2
    poggers thank you, that what i wanted to know