Collider2D.Cast does not seem to respect Collider2D.excludeLayers

Hi,

the way I understand Collider2D’s excludeLayers property is that the collider, including its Cast method, should not look for collisions on excluded layers. By that logic, the following code should never run the log line, whatever direction, distance or the excludeLayers mask might be:

Collider2D collider = gameObject.GetComponent<Collider2D>();
RaycastHit2D[] castResults = new RaycastHit2D[1];
int numCastResults = collider.Cast(direction, castResults, distance);
if (numCastResults == 1)
{
    if ((collider.excludeLayers & (1 << castResults[0].collider.gameObject.layer)) != 0)
    {
        Debug.Log("collision with an object on an excluded layer :/");
    }
}

I do encounter the log message though, when a game object on an excluded layer is in collision range. I’m sure I can work around it, I’d just like to understand where my misconception of excludeLayers and Cast lies.

None of the physics queries are anything to do with the Layer Collision Matrix or any specific overrides on a Rigidbody2D or Collider as that’s the whole point of them, they are a method for you to perform ad-hoc manual queries. Collider2D.Cast simply selects the shapes in that collider only.

You specify your own layers you are interested in; if not you’ll see in the docs that all queries default to Physics2D.AllLayers.

One thing that has been requested (but isn’t in there yet since we added the layer-overrides) is to be able to ask what effective layers are being used for a Rigidbody2D/Collider2D so you can use that as a LayerMask in your physics queries.

Thank you, that’s really unintuitive to me ^^ I’d expect my casts to follow the exact same collision rules that my object’s physics adhere to. Just a way to manually check collision instead of waiting for the collision to happen and reacting to events.

How so? The docs are my primary source of information and I’ve spent quite some time browsing them before I asked here. Neither the docs for Collider2D.Cast nor the docs for excludeLayers say anything about the fact the collision matrix and everything related only applies to physics simulation. Even if the Cast doc page included the default value for contactFilter (which it doesn’t), that still doesn’t mean that the excludeLayers mask isn’t consulted in addition to the filter.

In fact, I can’t find a source that has the info you just gave me, even though I know exactly what I’m looking for now. The closest I found is the use of layers doc page, which says:

If you don’t pass a LayerMask to the ray cast API call, Unity uses Physics.DefaultRaycastLayers which matches every layer except Ignore Raycast.
That’s for Physics.Raycast though, so not a Collider2D thing.

I guess we can move this post to be documentation feedback instead of a 2d physics question ^^

Oh my goodness, I only just realised that I was thinking about different API. I guess I was more tired over the weekend than I had realised (heavy cold over here right now). Sorry, we get so many posts asking about why doesn’t API Collider2D.Raycast or .OverlapBox (etc) not know about layers and it’s a general confusion and I kind of “read through” your post.

Ugh so yes, the first overload of Collider2D.Cast (without the ContactFilter2D arg) should use the settings of the Collider2D as it implicitly acts as the collider. The other overloads don’t do that because they have the ContactFilter2D which is you explicitly stating what contacts you’re interested in which includes LayerMask.

Would it be possible for you to submit a bug report for this to apply the fix against? That way you can track the fix.

Thanks for getting back to this, and get well soon! I can imagine, after 10 years and over 10k posts, there are quite a few reoccuring patterns in the question posts :stuck_out_tongue:

I’ve submitted a bug report, referencing this thread :slight_smile:

There are indeed and it makes you post-blind at times, sorry about that!

When you get the case/incident number back, please let me know that and I’ll see about fast-tracking it from QA.

Thanks for reporting!

Thanks, it’s IN-61268

1 Like