Help for raycasting 2d with multiple layers

Hello,

I have a background layer which takes all my camera background. In front of it I put some obstacles. I would like to add a pointer which only shows when the mouse is over the background part but not an obstacle. I send a Raycast2D only on the background layer, but since my background is also behind the obstacles, the raycast hits everywhere :frowning:

So I thought of 2 solutions

  1. Cast a RaycastAll and if the length is > 1, then I know there is an obstacle in front of the background
  2. Rework the background to not make it go behind the obstacles.

Those two solutions don’t seems to be the best. I’m sure there is something better but I don’t see it.

Do you have an idea ?

78115-raycast.png

Found this solution which I find cleaner :slight_smile:

((1<< hitInfo.transform.gameObject.layer) & layerMask) != 0

It works well, what do you think ?