🤔

Physics queries: LayerMasks#

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

information

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.

Resolution#

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.

warning

Double-check the mask value set in the inspector.

Or

🟢 Use LayerMask.GetMask#

Initialise and use a mask created using LayerMask.GetMask.

Or

🟢 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.