Temorarily Disable 2D Collision

Hey guys,

I am trying to create a Smash Bros. Style game where a player can jump on platforms from below and drop back down if he decides to no longer stand on the Plattform. For that I’d love to have a mechanic to drop a player from a platform if he holds ta certain key down.

I am however wondering how to do this, without throwing other players of the platform as well. I am currently using Physics2D.IgnoreCollision and slapping my player and the platform as well as the true flag in there, but I don’t know how to enable it once the player has dropped through. I don’t want to time it since the game is very fast paced. I would love to check for if the player is still touching the platform and if that is not true, I’d love to enable the collision again. Problem is, when I ignore collision, I obviously won’t get any collision to check if the player is still near the platform. :smile:

I hope someone can help me with this.

Here’s a bit of code I use to make the current logic happen:

    Collider2D currentPlatform;
    Collider2D playerCollider;
    private bool dropThroughPlatform;

    ...

   public void DropThroughPlatform(InputAction.CallbackContext context)
   {
        if (currentPlatform != null)
        {
            Debug.Log("Dropping");
            Physics2D.IgnoreCollision(playerCollider, currentPlatform, true);
        }
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        Debug.Log("Registered Plattform");
        currentPlatform = collision.collider;
    }

You can use queries to determine this

Use either Collider2D.OverlapCollider or Rigidbody2D.OverlapCollider with the appropriate ContactFilter2D that filters by the platform layer. You’d have to iterate the list of colliders returned to determine if it’s the platform collider/go though.

Maybe a better method would be to use Collider2D.Distance or Rigidbody2D.Distance specifying the platform collider. This gives you a ColliderDistance2D which gives you all the info you need including if it’s overlapped.

1 Like

Hey @MelvMay Thanks for the quick reply!

I tried it yesterday and have dabbled in this system a bit. Found out that the Collider2D.Distance works perfectly In my case! Thanks so much for your help and the fast response. It’s been so super helpful!

You’re most welcome, glad to help.