Unity - Understanding Collisions


Collisions in Unity are separated from the actual Sprite itself, attached as separate components and are calculated on their own. Let us now learn the cause behind this.

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.

We will go ahead and add a simple “wall” that our player character can collide against. To do so, create another sprite and scale it up using the Rect tool. We will also give it a red color through the Color property in the Sprite Renderer component.

Color Property

Now, go to Add Component in the Inspector, and type in “Box Collider 2D”. Click the first component that shows up, and a new component should appear.

Add Component

You will see a bright green line on the perimeter of your GameObject. This is the collision boundary. It is what defines the actual shape of the collidable objects.

Repeat the same with our movable GameObject as well.

Of course, collisions in Unity are not limited to simply boxes. They can range in a variety of shapes and sizes, and are not necessarily replicas of the object’s parameters.

Box Shaped

They can also take on polygonal shapes.

polygonal shapes

It is not uncommon to see developers and designers use approximate shapes in their collision boundaries to simplify their colliders and avoid unnecessary calculations for the engine. We will learn how to create different shapes and sizes with our colliders soon.

Now that we have our collision boundaries in place, hit play and see it in action.

Approximate Shapes

You will notice that our movable object is not behaving normal. We will discuss the behaviour of the object in our subsequent chapter.