# Unity collision detection 2D everything you need to know + examples

Here we are, I can feel another mystery, Unity collision detection 2D.

What? It’s too difficult this time!?

I don’t think so.

It’s a big case, I know, but if we want to create video games we need to face the truth:

Manage collisions.

Don’t worry we will find all the necessary information and this time we will have another partner to help for our investigation: Unity.

Yes, Unity will help us, then we just need to start analyzing the clues.

Step by step we will understand how to manage the collisions in our game and this monstrous case will become a lovely little kitten.

## Unity Collisions Vocabulary

Hey partner, here is what I already discovered, there are some important notions to know before proceeding and understanding Unity collision detection 2D:

### Physics Engine 2D

To detect collisions and simulate the real world physics system Unity provides a built-in physics engine, so all the maths behind acceleration, forces, gravity, collision detection etc… it’s already there.

Fewww! It seems other brave detectives already solved the case of physics! Thank you whoever you are!

### Collision Detection

It’s the detection of an intersection between two or multiple objects. Often it’s related to simulate the physical world in our game.

Let me list for you some detections we would like to handle:

• One object (or multiple objects) touches or hits another object (or multiple objects)

• One object (or multiple objects) overlaps another object (or multiple objects)

The detection means that in some way we need to be warned when a collision happens!

Please have a look at wikipedia for a large definition about collision detections in video games.

### Collider 2D

It’s a component to define the shape of a Gameobject for physical purposes.

If the Gameobject sprite is a complex drawing, just an approximation of its shape will be necessary to work, because the Collider 2D is invisible and not distinguishable during the gameplay!

So the sprite is the drawing, the collider is the shape, what does it mean?

The sprite is what the user sees, the collider is what the engine considers for collisions.

### Trigger 2D

It’s a particular behavior of a Collider 2D, when we simply want to detect when one collider overlaps another one without creating a collision.

So the object with this behavior stops being a solid object and allows other colliders to pass through.

### RigidBody 2D

It’s a component that allows the physics engine to control the object, it means it will be affected by gravity, forces and collisions too!

Are you ready to play with Vectors?

Right, to move our objects in different directions we need to manage vectors too! Why don’t you have a look at this article for a good refresh: Vector In Game Development: Understand The Basics Of Vector Math

While the physics engine 2D moves colliders and makes them interact with each other, the Rigidbody 2D component is in charge of the communication of these movements with the Transform components.

In this way the Gameobject will change according to the collider!

## How to…

### How can we add a Collider 2D or Rigidbody 2D to a Gameobject?

1- Select a Gameobject in the scene and click on add component.

2- Type “collider 2D” or “rigidbody 2D” in the search box and select the component (for Collider 2D we will see different types, we’re just going to talk about it).

### How can we set a collider as a trigger?

Just enabling the right checkbox property in the Collider 2D component.

## Which Collider 2D should we use?

Have you noticed there is more than one Collider 2D to choose from?

Yes, Unity provides us a list of different already pre-set Collider 2Ds.

Why?

Unity has an explanation for each one, we just need to enrich it with some helpful examples.

Let’s line up all the suspects then and have a look at each one of them (On the left is the sprite, on the right its collider)

### Box Collider 2D

Box collider 2D is for square and rectangle collision areas.

### Circle Collider 2D

Circle collider 2D is for circular collision areas.

### Capsule Collider 2D

Capsule collider 2D is for circular or lozenge-shaped collision areas.

### Edge Collider 2D

Edge collider 2D is for freeform collision areas and areas which aren’t completely enclosed (such as rounded convex corners).

### Composite Collider 2D

Composite collider 2D is for merging Box Collider 2Ds and Polygon Collider 2Ds.

### Tilemap Collider 2D

Tilemap Collider 2D is for each Tile set in the corresponding Tilemap component of a Tilemap Gameobject. We’ll talk about it in the Tilemap investigation!

## Rigidbody 2D Types

More than one Rigidbody 2D type? Are you sure? I knew it! Some witnesses saw something!

Let’s try to find out the version of the story of each Rigidbody 2D.

### Dynamic Rigidbody 2D

It’s the most common and most performance-expensive type, because it interacts with everything and it’s designed to move under simulation. It interacts with all the body types.

If we want a normal object affected by physics, this is our body!

The ball in this example has a dynamic Rigidbody 2D and it can be moved by applying forces and it’s affected by gravity!

### Static Rigidbody 2D

Opposite to dynamic, it’s designed to not move under simulation, Indeed it has infinite mass, heavy right? It only collides with dynamic Rigidbody 2D.

It is also the least resource-intensive body type to use.

You don’t really need to set the Rigidbody 2D for static objectsjust the collider 2D is necessary to detect collisions, but if a static object needs to be moved or reconfigured at run time, it is faster to do so when it has its own Rigidbody 2D and this will be our body!

Let’s add a box with static Rigidbody2D, it will be an obstacle to overcome!

### Kinematic Rigidbody 2D

Like the dynamic is designed to move under simulation, but only controlling it, it’s not affected by gravity and forces. It’s faster in terms of performance-expensive. It interacts only with dynamic body types.

If we want to move something not influenced by forces and gravity, remember it’s nature of interaction only with dynamic body types, this is our body!

What if  we add a kinematic Rigidbody 2D instead of the static one to the box? In this way we can control its movement without the influence of gravity. It will become a perfect movement trap for a platform video game!

## Collision Detection 2D in scripts

Well! Now we know many things about Unity collision detection 2D, we are ready to understand what we can do when a collision happens.

Unity, our partner provides us two different ways to handle the collisions depending if a collider is a trigger or not.

We just need to add the snippets below to the script of our Gameobject with a collider to handle the collisions and do what we want after the detection.

Just keep in mind if the trigger checkbox is unchecked we need the “Collision 2D” snippets, otherwise if not “Trigger 2D” .

### Collision 2D

```//Just hit another collider 2D
private void OnCollisionEnter2D(Collision2D collision)
{
//Do something
}

//Hitting a collider 2D
private void OnCollisionStay2D(Collision2D collision)
{
//Do something
}

//Just stop hitting a collider 2D
private void OnCollisionExit2D(Collision2D collision)
{
//Do something
}```

### Trigger 2D

```//Just overlapped a collider 2D
private void OnTriggerEnter2D(Collider2D collision)
{
//Do something
}

//Overlapping a collider 2D
private void OnTriggerStay2D(Collider2D collision)
{
//Do something
}

//Just stop overlapping a collider 2D
private void OnTriggerExit2D(Collider2D collision)
{
//Do something
}```

## Keep in mind… Mind the gap!

Hey partner! Unity collision detection 2D is not a secret anymore for us, but we can’t solve this case without considering these clues:

### Colliders 2D Properties

Each collider type has properties  over the trigger one, please check the documentation for each type to understand what these properties do (in the section “Which Collider 2D should we use” you can find a link for each collider type to its documentation).

### Rigidbody 2D properties

There are some properties like mass and gravity we will need to know, I suggest having a look at the official documentation for an explanation of all of them.

### Static collider 2D

It was “static Rigidbody 2D” wasn’t it? You’re right, but if we need just the collision detection without any reconfiguration or movement to place the object somewhere else, we can avoid adding the Rigidbody 2D component.

### Put something below or set the gravity value to 0 to avoid falling objects.

It’s a common mistake, when we add a dynamic or static Rigidbody 2D to an object, if we start the game the object will fall out of the screen. To avoid this we can either place a floor underneath with a collider, or set the gravity value attribute in the rigidbody component to 0.

### 2D is NOT 3D

As you already noticed Unity provides many of these components in 2 versions, for instance Box Collider 2D and Box Collider, well the difference is the second one is for 3D. So keep in mind to use only 2D components in 2D games, first for performances and second to avoid mixing things that won’t work like 3D components and 2D methods in scripts (OnCollisionEnter2D is NOT OnCollisionEnter).

### OnCollisionEnter2D won’t work if the trigger is enabled and vice versa.

Remember my friend, for collisions “OnCollisionEnter2D, OnCollisionStay2D, OnCollisionExit2D”, but if we enable the trigger behaviour in a collider they will stop working and we should use  “OnTriggerEnter2D, OnTriggerStay2D, OnTriggerExit2D”.

## Simple collisions 2D game

Hey! Look what I found in the secret files of the blog, An old record! Someone already tried to solve this case.

Here is my reconstruction of the record:

Whoever will be able to reproduce it will help to solve the lost case of collision 2D game.

Besides this they will win the best detective of the month prize!

Do you think you will be able to figure it out?

If not, don’t worry, you can find the complete project on GitHub, but promise me to look at it only as the last resort! 😉

## Conclusion

It was a hard job, we needed a lot of research, so let’s do a final recap:

• To detect collisions in a Gameobject we need a Collider 2D, with the right type according to our needs.
• To allow physics 2D to affect a Gameobject we need a RigidBody 2D.
• Keep in mind all the rules we discussed before configuring the game properly and you will avoid unwanted behaviors.

No collision fear anymore, we solved the case and acquired the knowledge required to create a wonderful video game with collision detection.

We are ready to solve the next case.

Goodbye partner, see you soon for the next job!

Written By
More from Romeo Violini

#### Gamification – What is it?

Nowadays the term “Gamification” has officially joined our vocabulary, but what is...