• Handle collision between two 2D Objects
Handle collision between two 2D Objects

Welcome to our Unity tutorial series.

Gamemakerlab guesses that you have done previous parts. Today, we will show you how to detect collision between two 2D objects.

When you make a simple game like pacman, you have to make the main character move around to find and eat cakes around it, but how do you know that your character is touching the cake or not? That’s when we have to handle a collision between two objects (your character and a cake).We will show you how to do it in Unity, it’s quite simple.

We will use the project from our previous tutorial. You can check it out or download only the project from this link.

In previous tutorials, we created and control animation for 2D object. Before we start to detect collision, we will add one more interesting thing to our pacman object.

By now, when you press left arrow key to make it move to the left, its face is still looking on the right, but we want it to look toward the direction that it’s moving to.

To do it, just open the MovingObject script then modify the code like this:

//Get horizontal direction

float x =Input.GetAxis("Horizontal")*movingSpeed*Time.deltaTime;

//Get vertical direction

float y =Input.GetAxis("Vertical")*movingSpeed*Time.deltaTime;

if(x !=0|| y !=0)


float angle = Mathf.Atan2(y, x)* Mathf.Rad2Deg;

transform.rotation=Quaternion.AngleAxis(angle, Vector3.forward);



else//The same as if (x == 0 && y == 0)




 As you can see, we just add two more lines of code inside the statement.

You will need to understand a little about mathematics here.

The first line is used to take the angle in radian between two directions that the object is moving toward then convert it to degree for rotating with the next line.The second line is used to rotate object with the pivot of object is the Vector3.forward (0,0,1), because we are working on 2D so we need to make sure the z coordinate is freeze.

 Now let’s save the script then come back to Unity and press play button to test it again.

If the pacman face is rotating like this when it’s moving up, then we are success to make it face toward when moving.


 Ok, that’s just a little thing but I think we should know how to make it.

 Now get back to our main goal, to detect collision between two objects.

We will use this image as my second sprite beside the pacman face.



Now just add it into our project. For anyone who doesn’t know how to do it, please check out other tutorials from our series.


After make it become sprite, add it into our scene. Put it in front of the pacman face like this:


 Now if you enter play mode and try to make pacman face eat the piece of pizza, nothing will happen.

To make it happen, you have to detect collision between two of them. We will show you how.

In Unity 2D game, when you want to detect collision between two objects, at least one of them need to be attached a component type Rigidbody2D. You can attach that component to the pacman object like this:

Select it in Hierarchy window -> Click on Add Component in Inspector window -> Select Physics 2D


 Then select Rigidbody2D

Because this game does not have floor for the pacman face to stand, and after you attached Rigidbody2D, the pacman object now have gravity so you have to set the Gravity Scale of this component to 0, if you don’t do this, your object will fall down when the game starts.

One more condition that Unity needs to detect collision that both two of them must have a component type collider. There are a lot of collider types, you have to choose the best type for your object depend on its shape.

In this tutorial, we will use two types of collider for this example: Circle Collider 2D for the pacman face and BoxCollider 2D for piece of pizza. It does not matter too much in this simple example but it’s just my selection. Remember there are other types for you to find out more.

Do the same steps as adding Rigidbody2D but select Circle Collider 2D for pacman objectand Box Collider2Dfor pizza object.

Set size of colliders smaller so it’ll be more real when the pacman face eats the piece of cake.

Change the Radius of Circle Collider 2D


Change the X size and Y sizeof Box Collider 2D


Now if we enter play mode and try to make pacman object move through the cake, it will get stuck. It’s because there is a collision between the pacman face and the cake. But how to make the pacman can eat that cake instead of get stuck when touching it? To do that we need to add some lines of code which will be executed when the collision happens.

Open the MovingObject script and add this lines to the end of class.

void OnCollisionEnter2D(Collision2D col)


//Remove the instance of the object that this object collides with


print("The cake was eaten by pacman...!");


 The OnCollisionEnter2D method is used to detect collision and the code inside it will be executed automatically when the collision happens.

The Collision2D parameter stands for the component type collider which was attached to the object that this object collides with.

Inside this method, the Destroy(GameObject) method is used to remove the instance of object from the game. In this case, we use it to remove the piece of cake and pretend like it was eaten by the pacman.

After that we will write a string on Console window to confirm that the cake was eaten, of course this is optional.

Now enter play mode to test and see the result when the pacman face touching the piece of cake.

If you see the piece of cake disappear and the Console window is showing like this:


This means we are success to help pacman eat that piece of cake.

Ok, so that’s is a simple way to detect a collision between 2 objects in 2D game with Unity. You can learn more about other types of collider. If you need any help, just leave us a comment. We will try to help you as much as we can.


Thanks for reading and see you again in our next tutorial.