Search Unity

Cant get RigidBody2D.Cast to return any collisions

Discussion in 'Physics' started by harkin1987, Jun 15, 2019.

  1. harkin1987

    harkin1987

    Joined:
    Jan 14, 2018
    Posts:
    2
    Hi all,

    First time post. Ive been stuck on this for a while now and hope somebody can help me.

    I have an object that I want to check if it is inside the bounds of a composite tilemap collider.

    My object is setup up with:
    - RigidBody2D kinematic
    - BoxCollider2D w/trigger

    No matter what direction or length I cast I cant return any hits.

    The code is as follows:

    Code (CSharp):
    1. Rigidbody2D rb2D;
    2.     protected RaycastHit2D[] hitbuffer = new RaycastHit2D[16];
    3.     protected RaycastHit2D[] hitbuffer2 = new RaycastHit2D[16];
    4.     protected RaycastHit2D[] hitbuffer3 = new RaycastHit2D[16];
    5.     protected ContactFilter2D contactFilter;
    6.     public bool playerInBounds = false;
    7.     public bool playerChecking = false;
    8.     public int origLayer;
    9.     public LayerMask layer;
    10.  
    11.     void Start()
    12.     {
    13.         rb2D = GetComponent<Rigidbody2D>();
    14.         origLayer = gameObject.layer;
    15.         contactFilter.useTriggers = true;
    16.         contactFilter.SetLayerMask(layer);
    17.         contactFilter.useLayerMask = true;
    18.     }
    19.  
    20.     private void Update()
    21.     {
    22.         List<RaycastHit2D> hitlist = CollisionDetection(Vector2.down);
    23.         Debug.Log("Casting down returned: " + hitlist.Count);
    24.     }
    25.  
    26.     public List<RaycastHit2D> CollisionDetection(Vector2 dir)
    27.     {
    28.         List<RaycastHit2D> hits = new List<RaycastHit2D>();
    29.         rb2D.Cast(dir, contactFilter, hitbuffer, 10f);
    30.         foreach (RaycastHit2D hit in hitbuffer)
    31.         {
    32.             if (hit.collider != null)
    33.             {
    34.                 hits.Add(hit);
    35.             }
    36.         }
    37.         Array.Clear(hitbuffer, 0, hitbuffer.Length);
    38.         return hits;
    39.     }
    Ive tried playing around with the contact filter, cast length etc to no avail.

    Any ideas?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    The main problem I can see is that you use ContactFilter2D.SetLayerMask() and pass a layer number and not a mask. If you didn't know, a bit-mask is a number where each bit is a flag for a specific layer therefore a 32-bit int means you can select 32 layers. Layer 7 would be a value of "1 << 7". Note that the docs for this also state that it automatically turns on "useLayerMask" when you use it so that's redundant although not problematic.

    Also, you seem to be producing a lot of garbage here by producing a "List<>". There's non-allocating versions of all 2D physics queries so you should perhaps consider using those too.

    Finally, I notice you're calling this each frame but unless you're updating physics manually each frame, it's redundant as the physics updates during fixed-update by default which isn't 1:1 with render frames.
     
    Last edited: Jun 17, 2019
  3. harkin1987

    harkin1987

    Joined:
    Jan 14, 2018
    Posts:
    2
    MelvMay that covers it nicely, sorted now! Thanks very much for the help kind sir.