Today I had a very frustrating experience with one of Unity’s new features. It shouldn’t have been this hard to make a raycast (2D) work. And I’ll be the first to admit, it was my fault. But in my defense I’ve been spoiled by Unity’s (usually) excellent UX.
This is a series of events that cost me over an hour of thorough debugging because of some some design decisions that… well, I don’t understand.
The new Physics2D raycast seems to have no option for both a layermask and a raycasthit output.
Well, I guess I need to use the new ContactFilter2D. I add the layers to the filter.layerMask field and put it into the raycast.
My ray collides with all layers, which is not what I wanted.
I spend a while making sure that all my layers are fine. Layers are a bit difficult, so I assume I did them wrong. The true bug didn’t occur to me because it just doesn’t make sense.
The filter.useLayerMask needs to be separately set to true even if the filter.layerMask contains something. (huh?) I fixed it.
Now my ray does not collide with triggers. Well, you can probably guess the problem, but again I didn’t realize and messed with physics settings while frantically googling for a bit.
It turns out if you use a ContactFilter2D it overrides your physics settings on raycasting and just ignores triggers unless you manually set it to hit triggers.
Well, this is not one of my shining moments of coding prowess. But it is not one of Unity’s shining moments either. Hidden defaults and unintended side effects are what I expect from my fellow game developers, not the tools I use.
It seems like useLayerMask does not need to be a public field. It should be set automatically when adding to the layerMask. It seems like useTriggers should default to the physics settings, not override them silently whenever you use ContactFilter2D.