Physics queries: LayerMasks#

Layer masks are bitmasks that describe which layers are active or otherwise.


Active layers will be hit by the query.

A layer mask may be an int, but it doesn't represent a single layer.

🔴 Incorrect#

int layerMask = 5;

🔴 Incorrect#

int layerMask = LayerMask.NameToLayer("Name");
NameToLayer returns a layer index, not a bitmask.


Correctly create a layer mask:

🟢 Serialize a LayerMask#

If bitmasks are confusing, a simple option is to serialize a LayerMask and configure it via the Inspector.

public LayerMask ExampleMask;

LayerMask can be passed to physics functions as it's implicitly convertible to int.


Double-check the mask value set in the inspector.


🟢 Use LayerMask.GetMask#

Initialise and use a mask created using LayerMask.GetMask.


🟢 Use bit shifting#

Manually create a mask from layer indices using bit shifting.

Then pass that mask to the correct parameter of the query.

I am still having problems with my query.