Unity LayerMask simplified

Unity LayerMask is used to include or exclude a layer from an operation in Unity. It’s mostly used to ignore or include a layer in Physics Raycast. LayerMask in Unity uses BitMask to either include or exclude a layer. In this tutorial, we will try to understand how Unity uses BitMask to create LayerMask and how you can include or exclude a layer using LayerMask.

Bits and LayerMask in Unity

Unity allows up to 32 Layers, out of which few layers are pre-defined by Unity. The user can configure the other layers.

Unity Layer number for LayerMask

You can include or exclude the layer by making the corresponding bit as true.

For example, you can include the first layer by either specifying the integer 1 with in binary is equal to 0001. Here is the table that you can use to include your layer

Layer NumberInteger /DecimalBinary Number/ LayerMask
010001
120010
240100
381000
41610000
532100000
6641000000
712810000000
8256100000000
95121000000000
10102410000000000

Using the left shift Operator to Assign the LayerMask

You are probably thinking how to remember this. Actually, there is no need to. You can use the left shift operator to get the desired result.

Let’s take the layer 6 for example, to include the layer in our operation we need to give an integer value of 64 to the Unity LayerMask which in turn converts it into BitMask “1000000”.

Since we know the layer number, we can left shift 1 by the layer number and achieve the same result.

Here is how it works. One in Binary number is “00000001”.

If you left shift one by 6 you get “01000000” which is equivalent to 64 in decimal.

In code it will look like this

using UnityEngine;

public class Mask_demo : MonoBehaviour
{
    int layer_number=6;
    LayerMask layer_mask;

    void Start()
    {
        layer_mask=1<<layer_number;
    }
    

}

Getting the Layer Number from Layer name

You get the layer number from the layer list in the Unity Editor Window. If you need the layer number during runtime then you can get it using the layer name.

LayerMask has a function called Name to layer that takes the Layer Name as a string input.

You can get the layer number as shown in the code below.

layer_number=LayerMask.NameToLayer("Ignore Raycast");
layer_mask=1<<layer_number;

Getting the Layer name from Layer Number

As we got the layer number from the layer name in the above example, you can also get the layer name from the layer number.

LayerMask has a function called Layer to Name for this.

Here is the code sample

Debug.Log(LayerMask.LayerToName(layer_number));

Inverting a Unity LayerMask

If you want to ignore a layer in the Raycast then you can invert the LayerMask.

This can be achieved using the Bitwise Complement operator in c#. it inverts every bit in the Layermask.

For example, if you have the BitMask value as 0100. Using the Bitwise compliment will convert it into 1011.

Passing the inverted LayerMask value will tell Unity to ignore that layer.

Here is how the code looks like

using UnityEngine;

public class Mask_demo : MonoBehaviour
{
    int layer_number=6;
    LayerMask layer_mask;

    void Start()
    {
        layer_mask=1<<layer_number;
        layer_mask=~layer_mask; //invert LayerMask
    }
    

}

Unity Raycast on specific layer

In our Unity Raycast tutorial, we saw how to ignore a layer during Raycast. Now let’s see how to Raycast only to a specific layer using Unity LayerMask

Here is the Raycast code

LayerMask layerMask = 1 << 5;//bit mask for layer number 5
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, 10, layerMask))

How to Check if a LayerMask contains a Layer?

If you want to know if your layer is in a LayerMask then you can use the inclusive OR operator to add a 1 to the LayerMask at the spot of the layer you want to check.

For example, if you want know if the Unity LayerMask contains the layer 2.

Let’s look at the code first

int layer_number=2;
if(layermask == (layermask | (1 << layer_number))
{
   //Your code
}

Say the LayerMask is 1010 and now we are using the inclusive OR operator on the LayerMask. Something like this “(1010 | 0100)”. The operator will apply the OR condition for each digit and return “1110”. This is not equal to the LayerMask, so the if statement will return false.

If the LayerMask is 1100 then (1100|0100) will return “1100” which is the same as the LayerMask and hence the if statement will return true.

Hope this article clears all your questions on Unity LayerMasks. If you have any other questions you can leave it in the comment box below.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from VionixStudio

Subscribe now to keep reading and get access to the full archive.

Continue reading