Many questions have been posted on this topic but I'm facing a weird behavior which I could not find an explanation for.
I perform a raycast giving it a LayerMask
parameter which is defined as a public variable and set in the inspector with the layers "Wall" and "Enemy" ticked.
At first glance it appeared to work fine because the raycast rightly stops on GameObjects with that layer.
But then I saw that it also stopped on GameObjects set to the default layer, as if the LayerMask was ignored. Is there something I'm missing?
According to this answer I have set up everything correctly and I expected it to work fine.
Looking at the inspector at runtime I can clearly see that my mask is set only to "Wall" and "Enemy", and that the GameObject the collider is attached to has default layer set. I'm then stating that the mask the way I'm using it does not exclude other layers. The only way I can get this working is by setting that GameObject layer to Ignore Raycast, but in this case the LayerMask is not necessary.
I'm calling the function this way:
RaycastHit2D inSight = Physics2D.Raycast(start, end, lm);
Maybe is there something different with Physics2D and raycasts?
I would really prefer to use LayerMasks instead of specifying Ignore Raycast on whatever I want to ignore. Any clue?