- Home /
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.
Answer by Bunny83 · Oct 06, 2015 at 02:22 AM
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.
I can't use a different layer because as soon as I put the object on another layer it just falls through the level (presumably because it's not on the same as the floors and stuff that makes up the level).
So how do I make the raycast ignore the actual object in such a case?
Your answer
![](https://koobas.hobune.stream/wayback/20220612040200im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to check if a raycasthit detects an angle less than 45 degrees and touches the ground 2 Answers
Subframe Collision Detection 0 Answers
making ray ignor certain collider 1 Answer
Obstacle avoidance 1 Answer
Destroy Turret with machine Gun 0 Answers