Physics.Raycast ignoring layermask; hits every layer.

            Ray ray = gameObject.GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out RaycastHit hitinfo, float.MaxValue,3))
                curm.movepos = new Vector3(hitinfo.point.x, curm.movepos.y,hitinfo.point.z);

This is my code.

3 is supposed to be the layer mask, it is only supposed to hit on colliders with the third layer. But, no, it doesn't. It hits every single layer. The only way to get it to not do this is to remove the colliders on other objects. I don't know what's going on, there's nothing that should be doing this, its just another headache unity tries to give me.

Pro tip to all: DONT enter integers as the layer mask, apparently it doesn't like to take in integers even though thats what the raycast says you can do, so, just put in a layermask

You can enter integers just fine, it's just that you're using it wrong (although your confusion is understandable!).

As explained in the manual, layer masks are bitmasks. This means that each bit in the number you pass represents the layer at that bit's position. Passing "3" (011 in binary) means you're telling the raycast to collide with layers 0 and 1 (from right to left, bit #0 is enabled and bit #1 is enabled).

More examples:
Passing "5" (101) affects layers 0 and 2
Passing "8" (1000) affects layer 3.
Passing "15" (1111) affects layers 0,1,2 and 3.
Passing "16" (10000) affects layer 4.

And so on. I'd recommend reading about bitwise operators and bit manipulation in general to learn more. This is extremely common in programming, not just a Unity thing.


1 Like

Some quick shortcuts converting Layer Numbers to LayerMask Values and other operations.

LayerMask mask = (1 << layerNumber);

LayerMask mask = (1 << layerNumberA) | (1 << layerNumberB) | (1 << layerNumberC);

LayerMask mask = (1 << layerNumber) | ((int)(layerMaskToInclude));

LayerMask mask = ((int)(layerMaskToInclude)) & ~((int)(layerMaskToExclude));

LayerMask mask = 0; // no layers

LayerMask mask = ~0; // all layers


It's a standard thing in computer programming/sciene called masking (bit masking):