0

Share

GAME DEVELOPMENT

How To Use Layers In Unity?


Layers in Unity indicate functionalities across game objects. It is used for building complex designs systematically and managing the order of game objects.

Agnesh Pipaliya

July 19, 2022

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.

Steps To Create Layers

Step 1

To Create Layer Go To Edit > Project Settings, Then Select The Tags And Layers Option.




Step 2

Select Any Of The Empty User Layers. Ex. - We Choose Layer 8.

Step 3

Type The Name Of the Layer That You Want In The Same Row Of Layer Index.




How To Assigning Layers To The Game Object

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.




How To Render Only A Part Of The Scene With The Camera’s Culling Mask

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

How To Casting Rays Selectively

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.

How To Make Layer-Based Collision Detection

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.

To Set Layer Collision Matrix Goto:

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

Change This To Suit Your Needs:

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.






Conclusion

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.  

img

Agnesh Pipaliya

COO of Vasundhara Infotech, a leading Software development company in the USA. His technological interests has helped the company in making useful decisions.

message

Have a project in mind? Drop a message to Bansi Pipaliya & start the discussion!

Get a Newsletter

Sign Up to our newsletter to get latest updates staight in your inbox.

Vasundhara respects your privancy. No Spam!

Get a Newsletter

Sign Up to our newsletter to get latest updates staight in your inbox.

Vasundhara respects your privancy. No Spam!

message

Have a project in mind? Drop a message to Bansi Pipaliya & start the discussion!

Latest 13 Web Development Trends To Expect In 2022
April 11, 2022 Category : company news

Revealing Vasundhara’s New Identity

Read More
Latest 13 Web Development Trends To Expect In 2022
May 17, 2022 Category : web development

Latest 13 Web Development Trends To Expect In 2022

Read More
Leave a Comment