Check if Layer is in Layermask?

How can you tell if a LayerMask contains a given layer.

I use this in C#: int layermask = LayerMask.NameToLayer("MyLayerName"); int layer = gameObject.layer; if ((layer != 0 && layermask == (layermask & layer))) { ... }

5 Answers

5

A C# solution that elegantly results in a bool is:

layermask == (layermask | (1 << layer))

This is beautiful. I added this as a static method on a utility class and it works like a charm public static bool IsInLayerMask(int layer, LayerMask layermask) { return layermask == (layermask | (1 << layer)); }

Thank you, this works nicely

Beautiful solution. Thank you.

Create a static class with extension methods as you go along. You’ll find lots of functionality you wish was included in the frameworks. Might as well add it yourself!

using UnityEngine;
using UnityEngine.Events;

public static class UnityExtensions{

/// <summary>
    /// Extension method to check if a layer is in a layermask
    /// </summary>
    /// <param name="mask"></param>
    /// <param name="layer"></param>
    /// <returns></returns>
    public static bool Contains(this LayerMask mask, int layer)
    {
        return mask == (mask | (1 << layer));
    }
}

Then just use it like:

if(_layerMask.Contains(otherLayer))
{
     //Do stuff
}

var layermask : LayerMask;
var layer : int;

function Start () {
    if (layermask.value; 1<<layer) {
        Debug.Log ("Layer is in layer mask");
    }
}

I'm not sure how to get this working. Is the & supposed to be &&? I'm also writing in C# (Forgot to mention that). What return values am I checking for on either side of the &?

Figured it out. Thanks! if ((layersCheckedForTargets.value & <<layer) == 0) {}

Er sorry for my application I needed to see if it was NOT in layer mask. Above would be != if you wanted to check if it WAS in layer mask.

Yeah, C# doesn't seem to cast to bool automatically like that.

What the hell is even that

if ((layerMask.layer & (1<<layerToCheck)) != 0)

Simpler and more straight forward if a single bit is true on both the mask and the layer to check, it contains the layer and returns non 0.

I think this is a better answer. However, the code contains a minor mistake. This a correctly working code: if ((layerMask & (1<<layerToCheck)) != 0) LayerMask implicitly converts itself to an int, so no need to use "layerMask.value".

There are two solutions provided above, one with “|” and one with “&”. The difference would be what you want the result to be when layer == 0 (Default layer), in which case the “|” routine returns true and the “&” routine returns false.

I my case I want to detect collision with a specific layer and in that case layer 0 should return false, so the “&” routine is the correct one for me. Put in a static method:

public static bool Contains(this LayerMask mask, int layer)
{
       return ((mask & (1 << layer)) != 0)
}

this is wrong. for layer == 0, (1 << layer) evaluates to 1. they both work. the difference is more conceptually. where the & variant checks if there is a non-empty intersection in the bit representation, the | variant checks if the intersection contains only elements from the first set. imho the &-variant is more ideomatic. It can also be used to see if 2 masks intersects (mask1 & mask2) != 0 whereas the |-version can't