Making raycast ignore multiple layers
I know how to make the raycast ignore a layer but I want it to ignore layers 9 and 10 but collide with the rest of the layers.
Currently, the raycast ignores layer 9.
// Bit shift the index of the layer (9) to get a bit mask
var layerMask = (1 << 9);
layerMask = ~layerMask;
if (Physics.Raycast(transform.position, transform.forward, Hit , Range, layerMask))
{
Answer by AlucardJay · Mar 14, 2013 at 06:39 PM
It is much easier to declare a variable that is typecast as LayerMask, then in the Inspector, simply tick the layers that you wish to be detected by the raycast. As a test, create a new script, at the top put
var myLayerMask : LayerMask;
Now save this script, go back to Unity and attach the script to an empty gameObject or the camera (it doesn't matter, this is just a test). Now look in the inspector, click on the drop-down menu then tick the layers you wish to be acknowledged. This is much easier than trying to implement the bitshift method in the documentation. Now you've seen how it works, simply use :
var myLayerMask : LayerMask;
// in a function
if ( Physics.Raycast(transform.position, transform.forward, Hit, Range, myLayerMask) )
{
// Do Stuff
}
http://answers.unity3d.com/questions/409360/raycasthit-behind-object.html
That's a great tip thanks! However if I wanted the Layer$$anonymous$$ask to be private, how would this be coded?
Is there a specific reason why you want this to be a private variable? As you are assigning the values in the Inspector, if you make it private then this ability is lost (private variables do not appear in the inspector, they are private), then you are back to the same problem where you have to use some bitshift calculations to select layers.
Unless, you really mean that you need a Private Variable and not a hidden Public one, you could first, and while the Variable is Public, select the Layers you desire in the Inspector, and then to use the "@HideInInspector", before the line you declare the Layer$$anonymous$$ask Variable. Notice that for each Variable you want to hide in the Inspector, you have to write the above statement. However, the whole method misses its usability, as alucardj already said, this way.
I would do it the other way round: declare the variable private and use the SerializeField attribute. This way you can edit the variable in the inspector and keep it private so other scripts can't directly access it.
Of course when you don't want to show it in the inspector (to be able to change the layermask) you have to choose the layers in your code manually.
I take the example from the OP:
var layer$$anonymous$$ask = (1 << 9);
layer$$anonymous$$ask |= (1 << 13);
layer$$anonymous$$ask |= (1 << 15);
layer$$anonymous$$ask = ~layer$$anonymous$$ask;
Or in one line:
var layer$$anonymous$$ask = ~( (1 << 9) | (1 << 13) | (1 << 15) );
This would exclude layer 9, 13 and 15.
$$anonymous$$eep in $$anonymous$$d that the IgnoreRaycastLayer should also be ignored. You can use the Physics.kIgnoreRaycastLayer
constant which is the layermask for the layer2 (has a value of 4 == 1 << 2 ). So just add this:
[...]
layer$$anonymous$$ask |= Physics.kIgnoreRaycastLayer;
layer$$anonymous$$ask = ~layer$$anonymous$$ask;
[...]
$$anonymous$$any Thanks! It Worked perfectly for me.
Answer by Spiff McShizly · Nov 25, 2014 at 01:43 PM
layerMask = 1 << LayerMask.NameToLayer ("layerX"); // only check for collisions with layerX
layerMask = ~(1 << LayerMask.NameToLayer ("layerX")); // ignore collisions with layerX
LayerMask layerMask = ~(1 << LayerMask.NameToLayer ("layerX") | 1 << LayerMask.NameToLayer ("layerY")); // ignore both layerX and layerY
Answer by Bryan-Legend · Apr 15, 2016 at 09:10 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))
Please don't post the same answer to several questions. Also your answer doesn't answer the question at all. The code in question does have the distance parameter setup correctly so this answer doesn't address the actual issue in this question.
If you want to add some hints you might want to post a comment next time.
Answer by guilhermereisrbm · Sep 17, 2017 at 03:51 PM
Here's how you ignore any layers you want:
using namespace System;
private int layerMask = Convert.ToInt32("11111111111111111111100011111111", 2);
this ignores layers 8,9,10 (where the zeroes are)
Remember the lowest order bit is zero-indexed - that means layer 8 is the 9th position from the right.
Uhm that's not really easier, is it? It's kinda hard to tell where layer 23 is.
Using a method like this would be better:
public static int CreateLayer$$anonymous$$ask(bool aExclude, params int[] aLayers)
{
int v = 0;
foreach(var L in aLayers)
v |= 1 << L;
if (aExclude)
v = ~v;
return v;
}
Now you can simply do:
int layer$$anonymous$$ask = CreateLayer$$anonymous$$ask(false, 2, 5, 7); // only cast against the layers 2, 5, 7 and ignore all others
int layer$$anonymous$$ask = CreateLayer$$anonymous$$ask(true, 0, 1, 6, 8); // cast against all layers except layers 0, 1, 6, 8
However if you like to work with 32 digit binary numbers, go for it -.- I think using hex constants would be more practical. Each hex digit stands for exact 4 bits.
0xFFFFFFFF // all layers
0x00000100 // only layer 8
0x00000300 // layer 8 and 9
0xFFFFFDFF // all layers except layer 9
Using the "Convert.ToInt32" solved my problem in just one line of code :) Thanks very much!!!
Answer by BLior · Apr 10, 2018 at 09:45 AM
The easiest way to choose is this I think:
And then you can check the layers you want in the inspector.[SerializeField] private LayerMask Name;
Your answer
Follow this Question
Related Questions
Make raycast ignore a layer? 1 Answer
How to make a user layer ignore raycasts of OnMouse* events? 1 Answer
Use Layer Collision Matrix for Raycast 1 Answer
Why won't Physics.RaycastNonAlloc mask layers properly? 1 Answer
Make everything above the floor my player is standing invisible when my camera cant see my character 0 Answers