Search Unity

LayerMask + RayCast does not work like described in the docs

Discussion in 'Scripting' started by Di_o, Sep 13, 2019.

  1. Di_o

    Di_o

    Joined:
    May 10, 2016
    Posts:
    62
    My goal is to cast a ray that ignores all Layers but one. This...

    https://docs.unity3d.com/ScriptReference/Physics.Raycast.html

    ... tells me I should do it like that:

    Code (CSharp):
    1.  
    2. private LayerMask layerMask = 1 << 9;
    3.  
    4. void Update(){
    5.    RaycastHit hit;
    6.    if (Physics.Raycast(origin.position, parent.transform.TransformDirection(Vector3.forward), out hit, layerMask))
    7.    {
    8.     print("I'm hitting layer 9 while ignoring all other layers"))
    9.    }
    10. }
    But if I have a collider with a different layer in front of layer 9, it still hits it. I'm confused...
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,411
    try if you add the distance parameter there?
     
    TomislavKlepac and Di_o like this.
  3. Di_o

    Di_o

    Joined:
    May 10, 2016
    Posts:
    62
    It seems to have done something, but now I'm getting a NullReferenceException.

    These are my LayerIDs: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 31

    So the layer I'm referencing exists...

    I even tried declaring it in Update() with the same result
     
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @Di_o

    Yes, what @mgear said the layer can accidentally replace the distance with this method overload:

    Physics.Raycast(origin.position, transform.forward, out hit, layerMask))


    So the last parameter here should be distance, and the layerMask (int) can be placed there too... but if you need to have layer mask, try this version instead:

    Physics.Raycast(transform.position, transform.forward, out hit, 100f, layerMask))
     
  5. Di_o

    Di_o

    Joined:
    May 10, 2016
    Posts:
    62

    A I got it now. I accidently copied the wrong line that caused the NullException. The distance did the trick.

    THX @mgear & @eses
     
  6. TomislavKlepac

    TomislavKlepac

    Joined:
    Apr 23, 2019
    Posts:
    2
    thank you very much, finally I understand what was wrong with my raycasts.
     
  7. Snowirbis

    Snowirbis

    Joined:
    Jan 30, 2018
    Posts:
    5
    Gosh this is stupid, this has to be a bug right ?
    I checked that my layermask is a int and not a float like the distance parameter, yet it doesn't work..
    Thanks for the workaround anyway !
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Physics.Raycast() is a HORRIBLY overloaded function. I recommend ALWAYS specifying the named arguments rather than hoping you got them in the correct order.

    This is because layerMask (an int) and maxDistance (a float) can be easily mistaken at the call site if you are passing an integer value for maxDistance. In this case, no error will be returned, and it will just fail mysteriously.

    The time you save by typing out named arguments will be your own.

    Instead of this:

    Code (csharp):
    1. Physics.Raycast(origin.position, transform.forward, out hit, layerMask)
    ALWAYS do this:

    Code (csharp):
    1. Physics.Raycast(
    2.      origin: origin.position,
    3.      direction: transform.forward,
    4.      hitInfo: out hit,
    5.      layerMask: layerMask)
    This will also reveal instantly if you have asked for a permutation of arguments that is not matched by the available overloads.

    EDIT: the same goes for constructing
    Ray()
    and
    Plane()
    objects: the identical-typed arguments can easily be inadvertently swapped, leading to mass confusion, especially when they sort of work near (0,0,0) for instance.
     
    Last edited: Apr 22, 2022
  9. Yodzilla

    Yodzilla

    Joined:
    Mar 21, 2013
    Posts:
    48
    Holy cow thanks for this tip. I just had a wild issue where I guess Unity thought the layerMask I was passing into the Raycast method was some other value so it wasn't doing what I expected. I honestly have never used that syntax for calling a method but The More You Know!
     
    DanjelRicci and Kurt-Dekker like this.
  10. valentin56610

    valentin56610

    Joined:
    Jan 22, 2019
    Posts:
    156
    anycolourulike likes this.