x


Using a bitwise operator with LayerMask

(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.:

if ((layerMask.value & hitObject.layer) == hitObject.layer)
    Debug.Log(hitObject.name + " is in one of our layers");

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?

(this.layers.value & hitObject.layer)
more ▼

asked Jul 30 '11 at 08:18 PM

Rafes gravatar image

Rafes
305 6 8 13

As a secondary question, could I just test to see if the return is > 0 to avoid a second look-up?

Jul 30 '11 at 08:18 PM Rafes

I like your function. So I made it a bit more compact and put it on a Tools class.

using UnityEngine;

public class Tools {

    public static bool IsInLayerMask(GameObject obj, LayerMask mask){
       return ((mask.value & (1 << obj.layer)) > 0);
    }
}

That way you can just access it from anywhere just by doing:

Tools.IsInLayerMask(myGameObject, myLayerMask);
Mar 07 at 12:10 AM Filippopotamus

That's what we do. Thanks for posting the compact version!

Mar 07 at 12:32 AM Rafes
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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:

/// <summary>
/// Checks if a GameObject is in a LayerMask
/// </summary>
/// <param name="obj">GameObject to test</param>
/// <param name="layerMask">LayerMask with all the layers to test against</param>
/// <returns>True if in any of the layers in the LayerMask</returns>
private bool IsInLayerMask(GameObject obj, LayerMask layerMask)
{
    // Convert the object's layer to a bitfield for comparison
    int objLayerMask = (1 << obj.layer);
    if ((layerMask.value & objLayerMask) > 0)  // Extra round brackets required!
        return true;
    else
        return false;
}
more ▼

answered Jul 30 '11 at 08:26 PM

Rafes gravatar image

Rafes
305 6 8 13

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)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x106
x42
x8

asked: Jul 30 '11 at 08:18 PM

Seen: 1863 times

Last Updated: Mar 07 at 12:37 AM