Raycast "layermask" parameter

The layer is a number from 0 to 31. It corresponds to each of those named layers.

A layer MASK is a 32-bit bitfield that says which of these you want to ignore. This enables you to ignore (mask) multiple layers, each one represented by a single bit in the 32-bit integer.

In your code above, 9 (Collision) is a layer, NOT a mask. Raycast takes a layermask.

If you want to turn a layer name into a LayerMask, here’s the utility you want:

You can also go cheap and cheerful and rotate the bits yourself:

int layer = 9;

int layerMask = 1 << layer;   // means take 1 and rotate it left by "layer" bit positions

That would cause layer 9 to be masked, ie,. selected to be hit by the raycast.

If you want to mask more than one layer, OR the bit-shifted results together:

int layerA = 9;
int layerB = 12;

int layerMaskCombined = (1 << layerA) | (1 << layerB);

To invert those masks (eg, take their opposite), use the tilde (~) (NOT A MINUS SIGN!) to flip every bit:

layerMask = ~layerMask;  // tilde ~ is bitwise flip

That will return things marked with layer 9 or layer 12.

I’m not going to try to add in graphics to show off bitfields… there’s TONs of bitfield diagrams and tutorials all over the internet. It’s the basic fundamentals of math on a digital computer.

EDIT: corrected 1/5/2023 in response to @Bunny83 below… thanks man!

16 Likes
How do I fix Raycast/BoxCast from colliding with other objects?
How to detect if an 2D sprite is stand on the ground
LineRenderer works fine (it's just me being silly)
Stop player standing in crawl space
Detect 3D Collision using filter
About how to avoid destruction. FPS and Bullets
How can only some triggers be ignored?
Need help accessing any layer in a script
LayerMask not getting assigned properly through script
[SOLVED] Raycasy2D not updating properly
Getting height from the ground using Raycasting - returning y = 0 no matter the height!
How to make specific Rigidbodies not interact with each other
Ignoring walls during ground check while using a boxcast
Using the same code as usual but cant jump [Please Help]
child gameObject keep its own transform!!
UIToolkit RenderTexture and UGUI World Space UI
Could someone please explain how does the AreaEffector2D-s .colliderMask works?
Raycasting is acting weird.
2D grapple hook
Near-Far interactor and GameObject Layer Issue
Spawn Objects On Specific Colours?
How to use multiple layer masks with a box cast.
BoxCollider2d include/exclude layers selects wrong layers?
Checking if a layer is in a layer mask?
A problem with a pickup script
How to make raycast pass through some colliders?
Physics.Spherecast not functioning?
Get the checked values in a multi-select TMP_Dropdown
Just realized that adding / in a layers name does something weird, how do I use this
Difference between "Layer index out of bounds" and Array IndexOutOfRangeException on mobile devices
Why can't I use a raycast while standing inside a collider?
Raycast 2D with Cinemachine issue
raycast layermask
Can you change collision mask of prefab in unity
What does "((characterController.collisionFlags & CollisionFlags.Below) != 0)" mean?
Linecast and LayerMask
ViewportPointToRay way to ignore collision on player object?