- Home /
Raycasting selectively ignore multiple layers?
Right now I'm using this to ignore the 9th layer, but how do I tell it to ignore both the 9th and 10th layers?
var LayerMask = 1 << 9;
LayerMask = ~LayerMask;
var hit : RaycastHit;
if (Physics.Raycast(transform.position, ShotDirection, hit, Range, LayerMask)) {
//SOMETHING!!!
}
Answer by robertbu · Jun 19, 2013 at 07:14 AM
One easy way is to use the LayerMask type:
var layerMask : LayerMask; //UnityScript
LayerMask layerMask; //C#
Now you can go to the inspector and set the layers you want. If you want to do it in code:
var layerMask = 1 << 9 | 1 << 10;
layerMask = ~layerMask;
It's saying that "~" cannot be used with "UnityEngine.Layer$$anonymous$$ask".
EDIT: Ah, I see it does not need that part, THAN$$anonymous$$S!
If your question is answered, please click on the checkmark next to the answer to close it out.
Answer by specialtactics1337 · Jul 14, 2021 at 07:35 AM
This doesn't seem to work with C#???
This works perfectly well with C#.
Could you provide an example of what you're having issues with
Alright, so I managed to get stuff working, but var layerMask : LayerMask; only returns errors. putting: var layerMask = 1 << 9 | 1 << 10; right before the raycast did the job alright. Thanks either way, mission accomplished :D
Your answer
Follow this Question
Related Questions
How do I use layermasks? 9 Answers
Raycast ignore layers except 1 Answer
Layer mask doesn't work... 1 Answer
Why is Raycast ignoring the layer mask? 1 Answer
How would I create a raycast that ignores Character Controller 3 Answers