Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

GetContacts() 2d should be this simple - why isn't it?

Discussion in '2D Experimental Preview' started by Xelnath, May 16, 2017.

  1. Xelnath

    Xelnath

    Joined:
    Jan 31, 2015
    Posts:
    402
    Code (CSharp):
    1.  
    2.         Collider2D grab = GrabHitbox.GetComponent<Collider2D>();
    3.         ContactFilter2D filter = new ContactFilter2D();
    4.         filter.useTriggers = true;
    5.         filter.useLayerMask = true;
    6.         filter.layerMask = LayerMask.NameToLayer("Playspace (Center)");      
    7.        
    8.         ContactPoint2D[] contacts = new ContactPoint2D[10];
    9.         int contactCount = grab.GetContacts(filter, contacts);
    10.  
    11.         for ( int i = 0; i < contactCount; i++ )
    12.         {
    13.             var contact = contacts[i];
    14.             if ( contact.otherCollider.tag == "CanGrab" )
    15.                 Debug.LogFormat("{0} can be grabbed.", contact.otherCollider.name );
    16.         }
    17.  
    18.         return false;
    19.     }
    20.  
    I've confirmed both the Grab collider and the target are both PolygonCollider2D - and are triggers... why doesn't this return *any* contacts?
     
    tonytopper likes this.
  2. Xelnath

    Xelnath

    Joined:
    Jan 31, 2015
    Posts:
    402
    It doesn't move relative to the rigid body at all.

    (This was relevant, because the Contacts array is cleared anytime the Collider is moved relative to the rigidbody)
     
    Last edited: May 16, 2017
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    It is simple but triggers do not produce contact points and they would never be used anyway. Use the overload of "GetContacts()" that returns Collider2D only which will include contacts with triggers. From your code, you should be using that anyway being as you don't seem interested in them and formatting them to be returned to you costs which is wasteful if you're not using them.

    I have no idea what that means.

    Also, this isn't an experimental feature, it's in 5.6 so please post on the general 2D or Physics forums from now on, thanks.
     
  4. Xelnath

    Xelnath

    Joined:
    Jan 31, 2015
    Posts:
    402
    Oh, okay. I didn't expect that GetContacts() would return 0 for one overload and > 0 for another. :x

    Trying now
     
  5. Xelnath

    Xelnath

    Joined:
    Jan 31, 2015
    Posts:
    402
    Okay. That worked!
     
    MelvMay likes this.
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    An overload is just a different function with the same name so it shouldn't be unexpected. It's why OnTrigger callbacks return the other collider only whereas OnCollision callbacks return a type that provides contact details etc.
     
  7. Xelnath

    Xelnath

    Joined:
    Jan 31, 2015
    Posts:
    402
    I don't want to spend a lot of energy defending my thickheadedness... but do you think that the discoverability of an extra version of GetContacts beats having something clearly named like GetIntersectingColliders() ?
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    A Contact is not a ContactPoint2D which I believe the assumption you're operating under. A contact in physics is when two things touch/overlap. Unity uses the terminology of Triggers and Collisions which are treated differently however a contact is both of them.

    "GetIntersectingColliders()" is just a verbose way of saying "GetContacts". "GetContacts" does not mean "GetContactPoints".

    The documentation separates them all however the one that returns ContactPoint2D should state that this won't include triggers as they do not have contact points.
     
    Deeeds likes this.
  9. Xelnath

    Xelnath

    Joined:
    Jan 31, 2015
    Posts:
    402
    Yeah, the terminology confused me. I was under the impression Contact == ContactPoint2D
     
    Deeeds likes this.
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    I have updated the API docs to include a section clearly stating that ContactPoint2D will not be returned for contacts involving a trigger. I've added that to all the GetContacts docs and the ContactPoint2D docs as well.

    Those changes won't appear immediately online, they go through the normal process for changes so will take a little while.
     
    Deeeds and Xelnath like this.
  11. Xelnath

    Xelnath

    Joined:
    Jan 31, 2015
    Posts:
    402
    Thank you. <3
     
    MelvMay likes this.