ObjectiveMain object of this blog post is to teach you how to have a collision between object1 and object2 but avoid the collision with object3 at the same time.
In Unity we often face issues regarding collision. If we want some game object to collide with another game object and avoid collision with the third game object at the same time then in this situation we make use of Layer and Tag concepts.
This blog teaches you how to have a collision between object1 and object2 but avoid the collision with object3 at the same time.
There are mainly 2 concepts for this in Unity:
The above two concepts are very useful to ignore collision with the third object in the same scene. So, Let us start with the first concept that is Layer.
Step 1 Using Layer
1.1 Create Project
Create one unity project and put 3 sprites into Assets >> Sprites folder.
1.2 Create Sprite
After creating a Sprite folder put any 3 sprites which you want to as an object. Here as an example I’ve put 3 sprites Ball, Green Pole, and Red Pole.
My aim is that the ball should collide with Red Pole but not with Green Pole.
1.3 Collision Detection
Now, save this Scene as a Game scene using ctrl+s (cmd+s). Here, take two more sprites one for setting the background of your scene named BG and second for standing base of all objects named Road. Now for collision detection, we must give collider and rigid body to all the sprites.
For that put your sprites into Unity Hierarchy panel and select Ball, Green Pole and Red Pole and give them a rigid body and appropriate colliders like box or circle using Add component in Inspector panel. Describes all the above stuff.
1.4 Make Script
Now, Let’s begin with coding and make one script for the movement of the ball: - left, right and jump using right, left and up arrow keys. Also add that script in ball using add component.
The above script is used for the movement of the ball. Let’s press the right arrow key and make movement of the ball it will collide with green pole and will not move ahead. Let’s jump on green pole using up arrow key it will collide with red pole.
Our aim is that ball should collide with red pole but avoid collision with green pole. For this purpose we use Layer Concept.
1.5 Make Layers
Make 3 layers named No Collision, Collision and Ball Layer using Layer button describe.
Now give different layers to different objects like:
Ball >> Ball Layer
Green Pole >> No Collision
Red Pole >> Collision - Using layer button in Inspector Panel.
1.6 Project Setting
Now go to Edit >> Project setting >> Physics 2D (if project is 2D else go to Physics) It displays layer hierarchy as shown in.
1.7 Physics Setting
Now, Uncheck layers which you don’t want to collide. Here we uncheck Ball Layer and No Collision Layer and then save the scene. Now, Ball will not collide with green pole but will collide with the red pole. Check it.
Step 2 Using Tag
2.1 Make Tag
Make one tag named NoCollide using tab button same as you added layer. Also assign green pole to NoCollide tag.
2.2 Change in Script
Open your ball movement script and make one method/function named OnCollisionEnter2D. Now, ball will not collide with green pole but will collide with red pole. Try it out!
Find whole project with different scenes:
- Using Layer Concept
- Using Tag Concept from here.
I hope you find this blog very helpful while working with Collision Using Layer and Tag Concept. Let me know if you have any question regarding Collision in Unity 3D please comment here. I will reply you ASAP
I am 3D game developer with an aspiration of learning new technology and creating a bright future in Information Technology.