Unity – Raycast LayerMask Exposed in Editor

      Comments Off on Unity – Raycast LayerMask Exposed in Editor

Dealing with raycast layer masks can be a bit of a hassle. Relying on hardcoded layermask numbers or names, bit shifting to get the behavior you expect; it can lead to some bad spaghetti code if you’re not careful.

Single layer mask:

Multiple masks:

Thankfully, the Unity Editor has a custom Layer selector that’s exposed to the editor by using the datatype LayerMask.

In the editor you can select as many masks as you want, or select none if that’s what you want.

Using the output is as simple as accessing the value property on the LayerMask instance.