Reverse LayerMask.

Hello!
I tried to use LayerMasks for my raycast and quickly realised it worked the opposite way than I needed it too. I need to exclude a layer but Layermask works the other way around. Is there a way to do this?

My current code is very simple:

        RaycastHit hit;
        LayerMask rayMask = 1 << LayerMask.NameToLayer("RootObject");
        if (Physics.Raycast(startPosition, startDirection, out hit,Mathf.Infinity, rayMask)){

I simply want to Ignore the “RootObject” layer in my Raycast.

Thanks in advance.

  • TwoTen
1 Like

Yup:

LayerMask rayMask = ~(1 << LayerMask.NameToLayer("RootObject"));
3 Likes

Did the trick. Thanks alot.