I’m trying to make a system in my first-person game so that you can’t interact with an object if it’s behind a wall relative to you, but it isn’t working aand I’m not sure why.
Here’s the code (the object of interaction is in user layer 8):
First, you only cast against objects on layer 8, which means if your wall is on another layer it will be ignored by your raycast. Make sure to also include the wall layer into the raycast mask like: 1 << (YOUR_WALL_LAYER_ID) | 1 << 8.
Second, use Debug.DrawRay to check if your ray is starting and heading where you expect it to.
When using a layer mask, rather define a LayerMask variable in your class. In the inspector you can then set the layers nicely without doing bit shifts. Then you can call Physics.Raycast(start, direction, range, myLayerMask)