- Home /
Raycast ignores my layer mask?
Hi, this is my first question in Unity Answers.
I want to do a ray cast against layer 9 so I wrote this piece of code
Ray r = camera.ScreenPointToRay(touchPosition);
RaycastHit hit;
int mask = (1 << 9);
if(Physics.Raycast(r, out hit, Mathf.Infinity, mask))
{
startPosition =touchPosition;
catchedObject = hit.transform;
Debug.Log("Touched object " + hit.transform.gameObject.name + " layer is " + hit.transform.gameObject.layer);
}
Surprisingly, the output is
Touched object MainCamera layer is 14
The Main Camera has a sphere collider with layer 14, but the ray cast mask should filter it, right?
So, where is the problem?
Are you colliding with a physics object or a trigger object?
I've found trigger objects seem to react oddly to the mask, if that helps.
Answer by Bryan-Legend · Apr 15, 2016 at 09:09 PM
Make sure you're not passing in the layer mask in the distance argument position.
Do This
if (Physics.Raycast(transform.position, transform.forward, out hit, Mathf.Infinity, SelectionLOSCheckMask))
NOT THIS
if (Physics.Raycast(transform.position, transform.forward, out hit, SelectionLOSCheckMask))
Duuddde thank you thank you, I have been trying to solve this for days and even though the documentation is different than this, this fixed everything, thank you!!
This is not the answer. It might be a helpful addition but has no relevance to the question as the OP actually used $$anonymous$$athf.Infinity in the original code. Specifically the OP used this version of Raycast:
public static bool Raycast(Ray ray, out RaycastHit hitInfo, float maxDistance, int layer$$anonymous$$ask)
and passed the parameters correctly.
omg dude thank u it worked; after 5 hours of fiddling, smashing my head on a locker, punching myself to the face, omg omg omg
It's 2018 and I had to stumble upon this to explains the weird collision issues I kept having. Thanks man.
Hell yes! $$anonymous$$y eyes totally danced over this. Thanks!
Answer by BBG · Oct 08, 2012 at 05:51 PM
mask is an int. It should be defined as type LayerMask.
Also it's easier to do :-
public LayerMask mask;
Then set the desired layer(s) in the inspector. Then you don't need to do the binary stuff.
As a new unity developer, I appreciate the tip about the public variable. I was struggling with a related issue where I was initializing the layermask variable in code and wondering why my changes didn't affect the program. Turns out that in the editor, the variable was already initialized there.
Answer by lloydv · Jun 22, 2021 at 01:34 AM
Make sure you're using LayerMask.GetMask
LayerMask.NameToLayer returns the index of the layer as seen in the layer inspector, not the mask. Pretty easy mistake to make, since it's the LayerMask class, so I hope Unity would update their comment for the NameToLayer method to make this clearer.
Thank you! That was exactly my problem, I changed for LayerMask.GetMask and it worked :)
Answer by foxor · Oct 09, 2012 at 05:01 AM
From the docs:
layerMask: A Layer mask that is used to selectively ignore colliders when casting a ray.
So you have created the mask that ignores only layer 9
try:
int mask = ~(1 << 9);
For the exact opposite meaning
This appears to be correct from my testing, despite the official documentation see$$anonymous$$g to indicate otherwise.
No, the layermask should have a 1 for each layer that should be tested and a 0 for each layer that should be ignored. You interpreted the sentence the wrong way.
The two constants in the Physics class are declared like this:
public const int kDefaultRaycastLayers = -5;
public const int kAllLayers = -1;
kDefaultRaycastLayers has the value 0xFFFFFFFB (all1 except the 3rd bit which is the ignoreraycast layer)
kAllLayers has the value of 0xFFFFFFFF
kDefaultRaycastLayers is used when you don't pass a layermask
You don't have to invert the mask after shifting to raycast a specific layer, see http://docs.unity3d.com/Documentation/Components/Layers.html for some good examples.
Answer by ShoutTree · May 17, 2020 at 03:09 PM
After a few tests, I think there's a bug for the Physics.Raycast api (the layerMask behaves abnormally).
I tested the one @Lone-Coder mentioned
Physics.Raycast(transform.position, transform.forward, out hit, Mathf.Infinity, SelectionLOSCheckMask
and the one the @chema.sre used in the question
public static bool Raycast(Ray ray, out RaycastHit hitInfo, float maxDistance, int layerMask)
Both failed for the correct layerMask. and if I do it like @foxor , it works.. But the result is not what I want since other objects of different layers will also return true if a ray intersection happens.
If I change to Collider.Raycast, there's no such problem.
Plus, I'm using Unity 2019.3.12f1
Your answer
Follow this Question
Related Questions
SphereCast doesn't work with a layerMask 1 Answer
unity raycast layers in Multiplayer 0 Answers
Layer Mask on raycast2d not working? 1 Answer
I have a problem with Layermask Unity2D 0 Answers
layers not ignored by raycast 1 Answer