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

Temorarily Disable 2D Collision

Discussion in '2D' started by bob_mosh, Jan 12, 2020.

  1. bob_mosh

    bob_mosh

    Joined:
    May 30, 2018
    Posts:
    12
    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. :D

    I hope someone can help me with this.

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

    Code (CSharp):
    1.  
    2.     Collider2D currentPlatform;
    3.     Collider2D playerCollider;
    4.     private bool dropThroughPlatform;
    5.  
    6.     ...
    7.  
    8.    public void DropThroughPlatform(InputAction.CallbackContext context)
    9.    {
    10.         if (currentPlatform != null)
    11.         {
    12.             Debug.Log("Dropping");
    13.             Physics2D.IgnoreCollision(playerCollider, currentPlatform, true);
    14.         }
    15.     }
    16.  
    17.     private void OnCollisionEnter2D(Collision2D collision)
    18.     {
    19.         Debug.Log("Registered Plattform");
    20.         currentPlatform = collision.collider;
    21.     }
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    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.
     
    bob_mosh likes this.
  3. bob_mosh

    bob_mosh

    Joined:
    May 30, 2018
    Posts:
    12
    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!
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    You're most welcome, glad to help.