Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

I can't figure out how layerMasks work on Raycast...

Discussion in 'Physics' started by georgeq, May 10, 2018.

  1. georgeq

    georgeq

    Joined:
    Mar 5, 2014
    Posts:
    275
    I have a script that takes a LayerMask as parameter:

    traction2.png

    Inside it I have this code:

    Code (CSharp):
    1. if(Physics.Raycast(Trans.position,-Trans.up,out hit,GroundLayer)) {
    2.             IsGrounded = hit.distance<=GroundDistance;
    3.             Debug.Log(hit.collider.gameObject.layer);
    4.          } else {
    5.             IsGrounded = false;
    6.          }
    According to the official documentation, on the layer mask parameter you specify which layer(s) should be included on the test, so if I specify "Road" only I should be seeing only 9's on the console, but I'm also getting 10's (Walls), which makes me doubt of the truthfulness of the manual... If I use the opposite setting (everything except "Road") I should be seeing only 10's on the console, but it remains empty, which means it's ignoring walls also. If I set it to everything except "Wall" it also ignores both. If I set it to "Wall" only get exactly the same result as with "Road". I also tested with "UI" only but got the same result. It seems like an everything of nothing situation.

    If I inspect the value of GroundLayer on the debugger I see 512 when it is set to "Road" only, which I understand, and -513 when is set to everything except "Road", which I don't understand.

    I'm confused I don't know if I'm missing something, if I'm doing something wrong or if it is a bug.
     
    Last edited: May 10, 2018
    Deeeds likes this.
  2. georgeq

    georgeq

    Joined:
    Mar 5, 2014
    Posts:
    275
    I made a test project in order to try to understand better what's happening and what I found is that you get two different results depending on how you set the layermask. If you set it to "Nothing" and then enable layers Raycast always hits the collider, however if you set it to "Everything" and then disable the layers one by one Raycast never hits the collider. I already filed a bug report: (Case 1036365)
     
    Deeeds likes this.
  3. SparrowsNest

    SparrowsNest

    Joined:
    Apr 6, 2017
    Posts:
    267
    The F***, how is this a thing?

    I mainly use layermasks for overlap sphere rather then raycast.
    Anyone one knows if there's a problem there as well?
    I'll try and do some testing when I have time
     
    Deeeds likes this.
  4. Deeeds

    Deeeds

    Joined:
    Mar 15, 2018
    Posts:
    436
    I'm sorry you must suffer in this way, @georgeq. You're helping me hold off on investigating raycasts for a bit longer...

    Godspeed!
     
  5. elcionap

    elcionap

    Joined:
    Jan 11, 2016
    Posts:
    1
    Hi,

    There is no Raycast overload with LayerMask without maxDistance. The culprit is a implicit cast in LayerMask to int and subsequently cast to float.
    So the overload is using your LayerMask as a maxDistance.

    []'s
     
  6. georgeq

    georgeq

    Joined:
    Mar 5, 2014
    Posts:
    275
    Wow... you are right! and I can't believe I didn't though of that. C# is so strict with data type conversions that one tends to forget implicit conversion is part of the language, thanks for the clarification. Sorry. All apologizes.