|
(I searched all over for this and couldn't find a simple Q&A) I'm trying to test if a GameObject.layer is in a LayerMask.value. e.g.: When I Debug.Log the test expression, it always prints 0. When try Debug.Log a simple test, such as 3 & 2, it prints 2. Any Idea why this comparison isn't working?
(comments are locked)
|
|
There is a great explanation here that I hadn't considered: http://answers.unity3d.com/questions/122586/layermask-vs-1ltlt-gameobjectlayer-.html I need to 'cast' the object's layer, which is the integer shown in the inspector, in to the masked version, which is used to compare with the LayerMask. For example, layer "6" in 1, 2, 3, 4, 5, 6** needs to be 1, 2, 4, 8, 16, 32** Here is a working function to do this: Its too bad I can't vote for my own post..hint, hint ;)
Jul 30 '11 at 08:42 PM
Rafes
Perfect! this answer helped me so much, thanks!
Feb 06 at 10:03 PM
chingwa
Very nice function. That helped me a lot.
Feb 11 at 03:43 AM
zangad
(comments are locked)
|

As a secondary question, could I just test to see if the return is > 0 to avoid a second look-up?
I like your function. So I made it a bit more compact and put it on a Tools class.
That way you can just access it from anywhere just by doing:
That's what we do. Thanks for posting the compact version!