T . Kathiravan
3 min readMay 1, 2021

--

Understanding Collisions

Collisions are Unity’s way of understanding the physical interaction between two Rigidbodies, and indeed, two gameObjects in general. Everything in your game is a GameObject.Even the individual tiles that make up your level are GameObjects by themselves. When we consider every component as a GameObject, we realize that there could be thousands of GameObjects in a scene, interacting with each other in some way. You can imagine that if Unity added collisions to every single GameObject, it would be impractical for the engine to calculate collisions for every single one of them. So there is a necessity to learn more about collisions between an object in Unity

Collider

In the search box add component boxcollider 2d and edit grass tile .When we play the object falls because of the influence of gravity.

To detect collision between two colliders ,both objects colliding should have the collider and atleast one of the objects should have Rigidbody attached as we are using 2D spirites , I have attached boxcollider 2d and Rigidbody 2D

Now drag the ball sprite and attach circle collider2d and also add rigidbody2d to make the ball fall to the ground due to Gravity. When it falls ,both ball and Grass sprites have two Rigidbodies attached and hence collision is detected but both object collides and react to physics and both falls down.This is not what we want , so I just remove Rigidbody2d component from the Grasssprite and now when we play you can see the object is collided as below.

Collider2

Now create Physics 2d Material for bouncy effects as shown below ..now the ball collide and bounce

Materials

Just creating some obstacles to show how the collider reacts with other Object

More Collider

We all done with colliders ..I have added coin to collect , so wen the ball jump and hit the coin, it collide with coin and jump to the next stage, but when the coin is checked with IsTrigger=true in the Inspector channel , though the collision is detected the ball pass through. as shown below

Trigger

Now open a script in the Script folder and attach it to the coin prefab and do as below.

Trigger Script

when we check on isTrigger property we need to use onTriggerEnter2d method to detect collison.In case we dont want to pass the collider but to detect collison we have to use onCollisionEnter2d .In the coin prefab uncheck isTrigger property so that collsion wont pass the collisder …check as below.

Collision2D

we can see details scripting for Collison and Trigger in the next article.

--

--