Unity Programming Tech-Tip: Raycast Layermask

Raycast Layermask

Refresh

First off a reminder on how raycast layermask works. The image below explains how you can bitshift a layer mask, for ignoring and checking against certain layers. The layer mask can also be set from the inspector, if a public layermask is declared.

Unity Programming Raycast Layermask

The issue

During development on Survive & Thrive I found a strange bug in the raycasting system of Unity. When I was testing a feature I wanted to ignore certain objects. Well that seems easy, just add a layer mask like so:

Unity Programming Raycast Layermask

Or at least that’s what I thought.

The Solution

The solution that I found was that you explicitly have to declare the ‘maxDistance’ for layer masks to work. Like this:

Unity Programming Raycast Layermask Solution

When the maxDistance is set as Infinity or another value the layer mask should work as expected.

 

So now you know. Please find me on our social media or via email if you have further questions.