Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved LayerMask not working

Discussion in 'Physics' started by georgeq, May 26, 2021.

Thread Status:
Not open for further replies.
  1. georgeq

    georgeq

    Joined:
    Mar 5, 2014
    Posts:
    662
    The RayCast documentation says:

    You may optionally provide a LayerMask, to filter out any Colliders you aren't interested in generating collisions with.

    but... I have this piece of code:

    Code (CSharp):
    1.       LayerMask mask = LayerMask.GetMask("Ground");
    2.       if(Physics.Raycast(ray,out hit,mask)) {
    3.          if(hit.collider.CompareTag("Ground")) {
    4.             return hit.point;
    5.          }
    6.          Debug.Log("not ground: "+LayerMask.LayerToName(hit.collider.gameObject.layer));
    7.       }
    which produces this output:

    layermask.png

    Unless I'm misunderstanding something, any colliders outside the Ground layer have to be excluded, and because of that, the only possible way to reach the Debug statement is when the collider is not tagged as "Ground" but still on the Ground layer. However, the log shows that colliders from other layers are also being hit.

    So the question is: is this a bug, or there's something I'm not seeing?
     
  2. georgeq

    georgeq

    Joined:
    Mar 5, 2014
    Posts:
    662
    My fault... for anyone having a similar problem, if you're using Visual Studio, do not, and I repeat, do not, trust blindly what intellisense tells you. When typing the raycast statement it showed me the next parameter was the layermask and I assumed that was correct, but no, it is the maximum distance.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,610
    I have never seen it give you the wrong arguments so any chance you can post a screenshot of this so we can see it. Should be simple if it's doing it.

    Note that there are multiple overloads so maybe that confused you: Physics.Raycast.
     
  4. georgeq

    georgeq

    Joined:
    Mar 5, 2014
    Posts:
    662
    intellisense.png

    It was my fault because I assumed IntelliSense was pointing to the right overload, and I didn't read the whole implementation, so when I saw "layerMask" I just went ahead and typed the corresponding parameter.
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,610
    Yeah, layer-mask not being a special type but rather an int is kind of a pain in this circrumstance as it naturually gets consumed by float args like distance. ;(
     
  6. OswaldP

    OswaldP

    Joined:
    Oct 15, 2015
    Posts:
    7
    THANK YOU so much ! This is the correct answer, keep an eye on the constructors of the Unity scripting API : https://docs.unity3d.com/ScriptReference/Physics.Raycast.html
     
    isitsou, DhiaSendi and Deleted User like this.
  7. Prune_Tracy

    Prune_Tracy

    Joined:
    May 24, 2020
    Posts:
    1
    This is still saving people from insanity, thanks dude!
     
    isitsou and PadraigCrowleyGames like this.
  8. citronovafuska

    citronovafuska

    Joined:
    Jul 7, 2020
    Posts:
    4
    Amazing, also was going nuts with this, then i read this, fixed the parameters and voala, everything working as expected.
     
    PadraigCrowleyGames likes this.
  9. Argus111

    Argus111

    Joined:
    Jul 4, 2022
    Posts:
    1
    Thank you!!!!!!
     
  10. Babyrager

    Babyrager

    Joined:
    Dec 10, 2020
    Posts:
    1
    Thx I was having the same problem
     
    PadraigCrowleyGames likes this.
Thread Status:
Not open for further replies.