I’m doing some raycasts to find out how a Rigidbody2D will move. That body is ignoring collisions with some colliders using Physics2D.IgnoreCollisions.
I first do a cast using the rigidbody’s Cast, which respects the IgnoreCollision settings. Then, if I hit, I do some extra raycasts to figure out more details about the surface. These does then hit the objects I’m ignoring, which isn’t what I want.
The obvious fix on my side is to check if the colliders hit with the raycasts are ignored using Physics2D.GetIgnoreCollision, but that’s a bit annoying to do. I can also do a multi-hit raycast, sort by hit distance and filter the hits, but that’s also not the greatest. I’d love for some way to ask Physics2D to do a cast using the filtering rules of an object. Is that in any way doable?
If I read you correctly, you’re using rb2d cast to learn about what your rb2d might do in the next X bits of time.
Based on that result you want to do some plain old p2d raycasts without having to go through the slog of getting the specific layermasking from the rb2d’s settings to the new raycast?
To me it just feels like you need a common source of truth for the ignored layers for this object, then use it when you set the rb2d ignores an also for the subsequent non-rb2d casts.
Is that what you are classifying as being a bit annoying? It definitely feels a bit two-pathy to me, but as long as the source of truth is a single quantity somewhere, I’m not sure how else to unify the layer-ignoring behavior of the rb2d cast and the p2d cast…
The problem here (explained for others reading it later that may not know) is that the “IgnoreCollider” is between colliders, not a query and a collider so beyond a few queries which implicitly refer to a specific collider or set of colliders, it doesn’t make sense.
To implement this, we’d need a reference to the first collider to use and I’m not sure there’s a great place to do that.
There’s the Collider2D.Raycast which in theory could be modified but it’ll change the existing behaviour so it’d need a global option which isn’t great.
The only other possible way would be to add this as an option to the ContactFilter2D. In that way, it’d be available to everything that used it which is almost all the queries. In some cases it would be redundant. The change would be along the lines of specifying a Collider2D to use as the IgnoreCollider. It’s still a little “ugly” though. It also might be confusing to some as they’d assume this would then follow the rules of the collision matrix.