While working on Unity, sometimes we have objects of the same group, on which we have to apply the same actions and effects or prevent them from receiving the same actions and effects. And for this purpose, layers are used in Unity.
Layers are useful features provided by Unity, which are widely used to render a part of the scene by cameras and by lights to illuminate only parts of the scene. Nowadays, layers also help in ignoring collision detection between game objects.
In this blog, you will learn how to create and use layers in Unity.
To Create Layer Go To Edit > Project Settings, Then Select The Tags And Layers Option.
Select Any Of The Empty User Layers. Ex. - We Choose Layer 8.
Type The Name Of the Layer That You Want In The Same Row Of Layer Index.
The select game object in Hierarchy that you want to apply a layer, goto Inspector window and click on Layer option and select layer name that you want to apply.
Ex - In my project, I have a gameobject name with Cube and select its layer is “My Player”.
Note that In the Tags and Layer window, the My Player layer is assigned to layer 8.
Culling mask is a camera’s property that allows us to selectively render objects which are in one particular layer. To do this, select the camera that should selectively render objects.
Now select and deselect the Layer name that you want to or not want to render by the camera.
In my case, I have a gameobject name “Cube” with layer “My Player” and I am just unchecking this layer in the camera’s culling property, so my game object was not renderer by the camera in-game view.
Note that the UI elements aren’t culled. Screen space canvas children do not respect the camera’s culling mask
Using layers you can ignore collider in a specific layer while rays are cast on that layer.
If you might want to cast a ray only against the player layer and ignore all other colliders, it is possible by applying different layers on a different game object physics. Raycast function takes a bitmask, where each bit determines if a layer will be ignored or not. If all bits in the layer mask are on, we will collide against all colliders. If the layerMask = 0, we will never find any collisions with the ray.
If we want to cast a ray against all colliders except those in the “My Player” layer.
Raycast will only ignore colliders that use the IgnoreRaycast layer when you don’t pass a layer mask to the Raycast function and this is the easiest way to ignore some colliders when casting a ray.
This way is used to make collision detection possible or not between the game objects.
The image above shows four GameObjects (3 planes, 1 cube) in the Scene view. A collision occurs when the physics engine detects that the colliders of two GameObjects make contact or overlap when at least one has a rigidbody component and is in motion.
Before we start the layer based collision, we need to define “Layer Collision Matrix”.
The Layer Collision Matrix defines which GameObjects can collide with which Layers.
Edit > Project Settings > Physics. (If working with 3D game object)
Edit > Project Settings > Physics 2D. (If working with 2D game object)
Select which layers on the Collision Matrix will interact with the other layers by checking them.
Note: In my case, the First Plane has layer “Layer 1”, Second Plane has layer “Layer 2”, Third Plane has layer “Layer 3”, and Cube has layer “My Player”.
In the example, the Layer Collision Matrix is set up so that only GameObjects that belong to the same layer can collide:
My Player is checked for My Player only
Layer 1 is checked for Layer 1 only
Layer 2 is checked for Layer 2 only
Layer 3 is checked for Layer 3 only
If, for example, you want My Player layer to collide with Layer 1 but not with Layer 2 and Layer 3, find the row for My Player, then check the boxes for the Layer 1 columns, and leave the Layer 2 and Layer 3 column checkbox blank.
If, for example, you want My Player to collide with Layer 2 but not with Layer 1 and Layer 3, find the row for My Player, then check the boxes for the Layer 2 columns, and leave the Layer 1 and Layer 3 column checkbox blank.
If, for example, you want My Player to collide with Layer 1, Layer 2 and Layer 3, find the row for my Player, then uncheck the boxes for the Layer 1, Layer 2, and Layer 3 columns.
This blog is on "Layers", the basic feature of Unity. Try and learn from the blog what a layer is and how you can use it in Unity. It will help you to make your complex designing process easy with layers (such as having steps to easily understand the process and work). It will make you easily work with the client's project.
COO of Vasundhara Infotech, a leading Software development company in the USA. His technological interests has helped the company in making useful decisions.
Sign Up to our newsletter to get latest updates staight in your inbox.
Vasundhara respects your privancy. No Spam!
Sign Up to our newsletter to get latest updates staight in your inbox.
Vasundhara respects your privancy. No Spam!