Understanding Layer Mask: Unity 5 Tutorial

Layer mask is a 32-bit flag which is represented by an integer. Bit shift is used to change the value of the layer mask flag.

Example:

Bit shift (C#)

32 Bit Flag

Integer Value

1<<0

0000 0000 0000 0000 0000 0000 0000 0001

1

1<<8

0000 0000 0000 0000 0000 0001 0000 0000

256

1<<9

0000 0000 0000 0000 0000 0010 0000 0000

512

In unity editor, at top right corner you can see the Layer menu. You can add and edit the layers by clicking on “Edit Layers” drop down selection. First 8 layers (0-7) are built-in layers that you cannot edit, but you add next 24 layers (8-31) that is called user layer.

Layer Mask - 1

Layer mask is very much useful when you want to raycast colliders of some particular layer. Let’s say, in a game you want to hit bullets only to enemy or any particular game objects then you can use layer mask.

Implementing Raycast using Layer Mask

Download Sample Project: LayerMaskDemo.zip

  • Create a sample project. Add three types of game objects in the scene. I have added Cube, Sphere and Cylinder in the scene.

2

  • Add three different layers for these three types of game object (Explained in start of this tutorial).
    3
  • Add suitable layers for each type of game object in unity editor. I am adding Cube layer for Cube type of game object and so on.
    AddLayer

Scenario 1: Hit only masked Layer Collider

  • Create a script “LayerMaskDemo” in the asset folder of the project. Edit the script as below given code and attach it to Main Camera game object.
  • Now run the application and click on the game objects in the scene. If the clicked game object will be cube the it will hide from the scene.
    Result 1
  • We can also get layer using name of the layer. NameToLayer and LayerToName method can be used to convert name of layer to its index and vice versa.
  • We can use logical “OR” operator to mask more than one layer. Edit the “LayerMaskDemo” script as given below.
  • Now run the application. Since we have masked both the cube and cylinder, so they will hide on click.
    Result 2

Scenario 2: Do not hit masked Layer Colliders

  • Layer mask can be also used if you want to hit all the other layer but not the masked layer. Change the script once again.
  • Run the code again. Now cube type game object will not hide but others will hide.
    Result 3

Hope you understand implementing layer mask in unity applications. Please post your comments for queries and feedback. Thanks for reading.

Share Button
The following two tabs change content below.
Gyanendu Shekhar is a technology enthusiast. He loves to learn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.

Gyanendu Shekhar

Gyanendu Shekhar is a technology enthusiast. He loves to learn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.

Leave a Reply

Your email address will not be published. Required fields are marked *