Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Reverse LayerMask.

Discussion in 'Scripting' started by TwoTen, Aug 11, 2016.

  1. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    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:
    Code (CSharp):
    1.         RaycastHit hit;
    2.         LayerMask rayMask = 1 << LayerMask.NameToLayer("RootObject");
    3.         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
     
    aloften likes this.
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,181
    Yup:

    Code (csharp):
    1. LayerMask rayMask = ~(1 << LayerMask.NameToLayer("RootObject"));
     
  3. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    Did the trick. Thanks alot.