Layer Masks in Unity

Tyler Smallwood
3 min readMay 10


Objective: Cover Layer Masks in Unity and how to use them in code.

In Unity we have Layers for the Physics system. We can use those same layers with Raycast by using LayerMasks. There are two ways to use Layer Masks in code. The first is by just assigning it. I’ll use a script that instantiates an object on the floor. That can be found in this article:

In Unity we will create a floor and go to Layer in the upper right of the inspector with the floor selected. To add new layers just select Add Layer.

Layer’s 0,1,2,4, and 5 are used by Unity.

We will also add in a wall with Layer set to Default. There should be a wall and floor.

Now in the script of spawning objects we will create a LayerMask variable that is SerialzedField.

We can set the LayerMask in the inspector now. Select Floor with it.

In the Physics.Raycast we just need to change the syntax a little to take in a LayerMask.

No LayerMask
With LayerMask

Now we can only Instantiate on the Floor and not the Wall.

The other way is to use a BitShift. This is because for Layers Unity uses 32 Bitmasks. When Unity interacts with something it checks the bitmask to see if there is a one or 0. To use a BitShift we just say set 1 at the 7th location. We know it’s 7 because that is where it is in the layer option menu.

In the Code all we need to do is remove _mask and set 1 << 7.

You can use LayerMasks in a lot of different situations. You could use it in a FPS shooter to avoid Friendly Fire. Check to see if what you are shooting at is a friend or foe. Thank you for reading and have a wonderful day.



Tyler Smallwood

I am passonate on learning to program and use Unity to become a skillful Unity Developer