Search Unity

get contact point returning null

Discussion in 'Physics' started by DownER149, Dec 24, 2018.

  1. DownER149

    DownER149

    Joined:
    Sep 11, 2018
    Posts:
    61
    the overlap collider works as intended , however when i try to retrieve the contact points everything returns null. what am i missing?



    Code (CSharp):
    1. ContactFilter2D contactFilter = new ContactFilter2D();
    2.             int layer = 12;
    3.             int layermask = 1 << layer;
    4.             contactFilter.SetLayerMask(layermask);
    5.             contactFilter.useLayerMask = true;
    6.             Collider2D[] colliders = new Collider2D[10];
    7.             int FFF =  GC.UnitPool[index].MoveRadius.OverlapCollider(contactFilter, colliders);
    8.             foreach (Collider2D FF in colliders) {
    9.                 if (FF != null) {
    10.                     GC.UnitPool[index].Col.Add(FF);
    11.                 }
    12.         }
    13.             //int GetContacts(Collider2D collider, Collider2D[] colliders);
    14.             ContactPoint2D[] Contact = new ContactPoint2D[10];
    15.             int FFFF = GC.UnitPool[index].MoveRadius.GetContacts(contactFilter,Contact);
    16.             foreach (ContactPoint2D SFS in Contact)
    17.             {
    18.                 Debug.Log(SFS);
    19.                 Debug.Log(SFS.point);
    20.                 Debug.Log(SFS.collider);
    21.                 Debug.Log(SFS.otherCollider);
    22.                 Instantiate(GOG, SFS.point , Quaternion.identity , GC.UnitPool[index].transform);
    23.             }
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,619
    https://docs.unity3d.com/ScriptReference/Collider2D.GetContacts.html

    Rather than iterating over the entire Contact array, you should only iterate over the number of items GetContacts() actually placed in that array, such as:
    Code (CSharp):
    1. int count = GC.UnitPool[index].MoveRadius.GetContacts(contactFilter,Contact);
    2. for(int n=0; n<count; ++n)
    3. {
    4.     var c = Contact[n];
    5.     ...
     
    DownER149 likes this.
  3. DownER149

    DownER149

    Joined:
    Sep 11, 2018
    Posts:
    61
    That is useful information, i was confused to why it was cast as a int in the first place, now it makes sense ,. thanks,
    i wasn't ever able to get the contact points to work. instead i switched to getting the bounds of the collider, and work perfectly for what i intended.