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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Collider2D.GetContacts always returns empty.

Discussion in 'Physics' started by jaclark, May 16, 2020.

  1. jaclark

    jaclark

    Joined:
    Sep 29, 2014
    Posts:
    19
    Hey there,

    I'm building a tool where I place a bunch of objects on a grid and then remove some if they're colliding with other types of objects. It seems like Collider2D.GetContacts would be ideal for what I'm trying to do, but it's not detecting any Contacts. I'm working with a bunch of overlapping Polygon & BoxCollider2Ds, none of which are triggers or rigidbodies.

    Here's the code I'm working with:

    Code (CSharp):
    1.  
    2. Collider2D[] hits = new Collider2D[100];
    3. for (int i = 0; i < _plants.GetLength(0); i++)
    4. {
    5.    for (int j = 0; j < _plants.GetLength(1); j++)
    6.    {
    7.       ActivePlant plant = _plants[i, j];
    8.       if(plant == null)
    9.          continue;
    10.      
    11.       Collider2D collider = plant.GetComponent<Collider2D>();
    12.       int hitCount = collider.GetContacts(hits);
    13.      
    14.       // if hitting Rocks or Water, destroy
    15.    }
    16. }
    17.  
    I've also tried Collider2D.Cast with no direction/distance, and that also didn't work. Collider2D.Raycast works, on the other hand, but it's not accurate enough; it's just casting a point. It does tell me, though, that *some* 2D physics are working.

    I've been off of 2D development & Unity's Physics systems for a while, and I'm digging back into them now, so I expect that I'm just making a dumb mistake or forgot a setup step. Any help is appreciated!
     
  2. jaclark

    jaclark

    Joined:
    Sep 29, 2014
    Posts:
    19
    I've figured it out. It was a misunderstanding of what 'GetContacts' was referring to. 'OverlapCollider' is what I was looking for, as that returns all of the other colliders that overlap an object. GetContacts must refer more to contact points in simulation, rather than overlaps.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,563
    As per the docs, there's overloads to return the ContactPoint2D or if you're not interested in the contact details, only the Collider2D so you can do both. You can get results into an array or a List<T> which will be resized for you.

    GetContacts returns contacts calculated during the last simulations step, it doesn't calculate them there and then so you probably don't have any because these "plants" don't have any.

    OverlapCollider is a spatial query that performs the work when you call it and doesn't relate to contacts. It looks at the geometry of the collider and finds other collider geometry it overlaps so is more expensive than asking about pre-existing contacts.

    Note that there are much simpler queries to use if all you want to know is if something IsTouching, especially if you use layers to separate these things.

    You need to determine whether you have contacts (or need them) and use the appropriate query.

    It's not clear what you mean here. If none of these colliders have a Rigidbody2D then you mean they are implicitly Static (non-moving). Static do not produce contacts with other Static for obvious reasons.

    If you pause your game, you can select any Rigidbody2D or Collider2D and in the inspector go to "Info > Contacts" and it lists all the contacts. If you have none then that's why and you need to configure it so you have contacts.
     
    Last edited: May 17, 2020