Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by cosmos · Aug 09, 2019 at 12:01 PM · raycastunityeditorvisual studiolayerslayermask

Layer mask doesn't work...

Hello i'm using a layer mask while ray casting but whatever i do it doesn't ignore the given layer. I used this method before & was working fine. Guess there is a problem that a i cannot resolve. Here is how i use the method:

     // Ignore layers 21 & 0
     int layerMask = ~LayerMask.GetMask(LayerMask.LayerToName(21), LayerMask.LayerToName(0));
     if (Physics.Raycast(position, direction, out hit, maxDistance, layerMask)) {
         // Always reaching this point where hit.transform.gameObject.layer = 21
         if (XLayers.shouldDoStuff(hit.transform.gameObject.layer)) {
           // DO stuff
        } else{ // other stuff }
     } else { some other stuff }

As far as i know this symbol ( ~ ) makes it ignore the given layers. I tried to remove it too the result is the same. Also i tried to send 0 as the layer mask which means ignore all layers, i this case the ray collides with nothing as expected . Any advice appreciated thanks.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hellium · Aug 09, 2019 at 12:09 PM 0
Share

Try :

 int layer$$anonymous$$ask = ( 1 << 0 ) | ( 1 << 21 );
 layer$$anonymous$$ask = ~layer$$anonymous$$ask;
avatar image cosmos Hellium · Aug 09, 2019 at 12:25 PM 0
Share

Thx for suggestion but the result is the same. Hit layer is 21 :D

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by cosmos · Aug 09, 2019 at 12:40 PM

Got it. This is my prefab:

 ParentGameObject ( Layer : 21, has rigidBody, has collider )
     ChildGameObject ( Layer : 18, no ridigBody, has collider )

The parent layer is ignored as it should be & the child is collided with the ray.

BUT hit.transform.gameObject.layer returns you the layer of the gameObject that CONTAINS THE RIGIDBODY. Which is the ParentGameObject. Doesn't matter what layer is the game object contaning the collider.

For the solution I added a rigidbody to the ChildGameObject it works now.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Aug 09, 2019 at 01:07 PM 2
Share

Uhm, that's why the RaycastHit structure actually has a collider field as well which contains a reference to the collider that was hit.

avatar image cosmos Bunny83 · Aug 09, 2019 at 01:55 PM 0
Share

Nice catch. I didn't noticed that until now.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

161 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Help with Layermasks 1 Answer

How do I use layermasks? 9 Answers

Layer Mask Detection 2 Answers

Control "Layers" dropdown from UnityEditor script 1 Answer

Why is Raycast ignoring the layer mask? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges