- Home /
Layermask (raycast) wont work...
I am using the following code to detect when the player mouses over any active game objects in the layer tilesLayer:
Ray ray = mainCam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
//if any tileNode collider is moused over...
if(Physics.Raycast(ray, out hit, 500f, tilesLayer)) {
If I remove the tilesLayer parameter, the raycast picks up the specific tiles I'm trying to detect mousing over. However when I use the parameter tilesLayer (which should be a constant value of 8, as that is the layer for all of my tiles), it does not detect the tiles at all... I even added this code when trying without the parameter:
Debug.Log ("Currently moused over: " + hit.collider.gameObject.name + ", on layer: " + hit.collider.gameObject.layer);
And it said "Currently moused over: tile117, on layer: 8". Yet if I use 8 as the raycast layermask parameter it doesnt detect the tiles... What's going on???
Answer by Clet_ · Jun 02, 2014 at 12:18 AM
This is a layerMASK, therefore it MASKS the desired layer for raycast. If you want to only include tilesLayer, use ~tilesLayer.
The ~ operator is a bit-wise NOT. It will turn all the binary 1s into 0s and vice-versa. By doing it, you tell the raycast to ignore every layer, except the one that you specified. In this case, tilesLayer
OH okay, thank you very much! I read the documentation today and had no idea what bitwise meant..
this worked perfectly, thank you very much! :)
Please mark this answer as correct if it solved your problem. That way, other people encountering this type of situation can know how to fix it.
To accept an answers as correct, tick the checkmark on the left hand side of the answer
Sorry, I was in a hearthstone match when I read your answer. $$anonymous$$ade sure to come back and mark it as the answer and give the thumbs up! However I'm still having an issue. I added ~8 as the layermask parameter for the raycast, yet it's picking up gameObjects in layer 10... Why and how do i stop that?
EDIT: and it detected a gameobject on layer 0 as well
As I said, Layers works on bits. So if your layer number is 8, this means that the 8th bit is 1, and the other 31 are 0s. (That's why you can only assigne 32 layers, it uses an Int32 to store them).
In order to mask correctly, you need to do
~(1 << Layer$$anonymous$$ask.NameToLayer("$$anonymous$$yLayerName"))
or
1 << 8 //If you happen to know the layer number.
operators are called bit shift operators. a << b means all bits in a are moved left b times. By doing 1 << 8, you go from 000000001 to 100000000. ~(1 << 8) results in 011111111. This means the raycast will ignore every layer BUT the 8th one
If you want to raycast on multiple layer, you can do something like that :
~( (1 << Layer$$anonymous$$ask.NameToLayer ("$$anonymous$$yLayer1")) | (1 << Layer$$anonymous$$ask.NameToLayer ("$$anonymous$$yLayer2")))
This line would make sure the raycast is only casted on $$anonymous$$yLayer1 and $$anonymous$$yLayer2
I'd suggest to take a look into bit-wise operator if you want to fully understand Layer$$anonymous$$ask. It can be hard to understand at first, but it is a very compact and efficient way to do it.
Answer by DadenNash · Jan 29, 2019 at 04:30 PM
How was this an accepted answer when the documentation says the opposite. https://docs.unity3d.com/ScriptReference/Physics.Raycast.html
This fixed my issue - https://answers.unity.com/questions/329389/raycast-ignores-my-layer-mask.html
Yeah, it doesn't make any sense at all. ~ excludes only that layer, it does the opposite of the accepted answer. It not only wasn't helpful, it just added more issues for the OP.
The answer you linked to also solved my issue as well, so thank you!
Answer by TomPo · Nov 21, 2017 at 11:06 AM
Or just make LayerMask myLayer; public so you will have nice scroll menu in the inspector and then you can choose what you want.
Answer by ginryu · Nov 05, 2021 at 01:45 PM
I found an important thing about layermasking, layermasking seems to fail on even number layers. the time this happenes is when I bitshift int layerMask = gameObject.layer << gameObject.layer; then I use layerMask = ~layerMask; and use that in the layerMask slot of the raycast function, when tested, I found that the ray would ignore all odd numbered layers 1,3,5,7,9, etc. but the layermask would fail on even layers 2,4,6,8,10 etc