Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by microp · May 27, 2011 at 03:10 AM · layerlayermask

LayerMask vs 1<< gameObject.layer ?

I need to do a raycast on a specific layer. I thought I'd use gameObject.layer and build the mask myself, but I don't get the same value as if I use a LayerMask and pick the layer manually. Ideally I'd prefer using the object's layer than having to add another inspector member.

I couldn't find any explanation for my results. According to the documentation, 1 << layer should work. Any idea? Thanks!

EDIT: Here's some additional information as requested by, erm, FriskyDingo.

int raycastLayerMask = 1 << gameObject.layer;

Physics.Raycast(..., raycastLayerMask);

gameObject.layer is 15, which corresponds to the correct layer number in the layer settings. Using this method, 1 << 15 is 32768. However, using a LayerMask, the int value when I check layer 15 is 1024. 2^10 is 1024.

The thing is, the bit shifting results seem more logical to me, but the LayerMask seems to be right. My raycast hits the expected object when layerMask is 1024, but not 32768.

Where does that come from? I have no idea. There doesn't seem to be any relation with the built-in layers either.

So there. I'm stumped! :)

Comment
Add comment · Show 3
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 FriskyDingo · May 27, 2011 at 10:54 AM 1
Share

Can you include the offending code, the results you're getting and the results you expected?

avatar image microp · May 27, 2011 at 06:50 PM 0
Share

Of course. I'll edit the question.

avatar image microp · May 27, 2011 at 07:20 PM 0
Share

Ah, I think I might've found the trouble. I'm an idiot.

2 Replies

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

Answer by Owen-Reynolds · May 27, 2011 at 04:18 PM

The problem is that humans think of layers as 0, 1, 2, 3, 4 but computers think of them as 1, 2, 4, 8, 16 (in binary, 1, 10, 100, 1000, 10000.) You just need to keep straight whether it expects a human layer or a computer layer:

o gameObject.layer uses human numbers. When you set gameObject.layer=10 and print, it tells you 10, but it internally sets layer to 1024 (in other words, the GET and SET for layer are doing a lot of work.)

o Raycast uses computer numbers, for speed. Both of these check against layer 2 (Ignore Raycast)

 RayCast(.... (1<<2));
 RayCast(....  4);

When you use a human number where it expects a computer one, or vise-versa, randomish things happen:

 // ERROR:
 RayCast(.... thingToCheck.layer); // if layer is 12 (1100) you are really doing a
                                   // raycast on layers 2 and 3
 // WORKS! convert from human 1,2,3 back to computer 2,4,8:
 RayCast(.... (1<<thingToCheck.layer));

I'd say if you want to use gameObject.layer directly for raycast masks, best thing to do is precompute the real one:

 int catLyrMask = (1<<someCat.layer); // in Start?

 RayCast(.....catLyrMask);
Comment
Add comment · Show 3 · 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 microp · May 27, 2011 at 07:05 PM 0
Share

That's pretty much how I understood the issue. Raycast takes a mask, while GameObject.layer is a simple number, hence the need to bit shift. I edit the question with more details. I'll keep looking at it, maybe there's something else wrong in my code.

avatar image microp · May 27, 2011 at 08:18 PM 0
Share

I found the problem in my code. I still gave your answer a thumbs up because it's a pretty damn thorough explanation of masks that will apply to more people than my own answer will. Thanks!

avatar image Rafes · Jul 30, 2011 at 08:45 PM 0
Share

I ran across something similar and posted it here (with resulting function to handle the test) http://answers.unity3d.com/questions/150690/using-a-bitwise-operator-with-layermask.html?viewedQuestions=122586&viewedQuestions=150690

avatar image
0

Answer by microp · May 27, 2011 at 07:26 PM

Well that's embarassing. Yet again! Anyway, the thing is, I'm trying to use the object's layer as if it were the whole layer collision matrix. Of course it doesn't work. I'm telling Physics.Raycast() to collide against the object from which I'm casting the ray, rather than using the collision matrix associated with its layer.

As there's no way (that I know of) to get the collision matrix from a script, I guess I'll have to leave the LayerMask exposed.

Thanks for your help!

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Dealing with multiple crosshairs 0 Answers

OverlapSphere ignoring all colliders when I use the layerMask parameter. 1 Answer

Differentiate between layers in LayerMask. 2 Answers

AI Still Sees The Player When Layer Has Changed 1 Answer

Layermask (raycast) wont work... 4 Answers


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