How to make Raycast ignore object NOT on the Ignore Raycast layer?

I have a raycast that is cast from the camera attached to the player in a first person game I’m making that is used to interact with the environment. The problem I’m having, however, is that when you look down enough, the Raycast hits the player object. I am aware you can put the player on the Ignore raycast layer, however the thing is there are key mechanics that are completely dependent on raycasts hitting the player. So my question is, how do I tell this raycast to ignore the player? I am away of the Physics.IgnoreCollisions function but I can’t seem to get it to work properly.

You can only ignore things that are on a specific layer. It doesn’t have to be the IgnoreRaycast layer but it has to be “some” layer. You can only ignore the whole layer, not just one or some objects on that layer. You can ignore as many layers as you want.

Likewise you can raycast only against object that are on a specific layer. This is usually the more common approach. If for example you want to find enemies you could put all your enemies on a seperate enemy layer. When you do the raycast you can pass a layermask as parameter which specifies against which layers you want to raycast.

The easiest way to define a layer mask is to create a public variable of type LayerMask. That way you can choose which layers you want inside the inspector.