Christopher Graf

May 10, 2021

2 min read

Unity Development — Collisions

So you’ve been following the Unity Developer Journey and have read that rigidbodies are necessary for collisions, but wanted to learn more about these collisions. No time like the present to dive right in.

Which way are they going to collide?

There are two types of collisions in Unity that we are going to talk about. These are regular collisions and triggers. A regular collision is exactly what you imagine it would be. A car hits another car, so it skids in the direction of the force. Someone pushes someone else and they fall. That is a regular collision.

A collision of two cubes

A trigger is when there is no physical force applied, but an event occurs because they touched. While these don’t really exist in real life, some perfect examples in video games include collectibles (Mario collecting coins and Sonic grabbing rings) or other pickups (Walking over rupees in The Legend of Zelda and apples in Crash Bandicoot).

One cube passing through another and a ‘trigger’

Besides a Rigidbody, the other thing you need to perform these collisions is a collider component. Usually this is determined by the shape of the object (box collider, capsule collider, etc). They all have the option of whether or not to be a trigger. This would mean that when another object hits they’re collider, it would not act as if hitting a physical object. It would simply go through. With these two distinctions, we can actually get into the code as well.

A standard Box Collider with the ‘Is Trigger’ option ticked

On the object with the Rigidbody and collider, you can add OnCollisionEnter() or OnTriggerEnter() to their script. Try to remember the examples given to determine which is the proper way to go for your situation.

The “other” in both cases is the object that has collided or triggered this object

If you want an enemy missile to knock your vehicle with physical force, use OnCollisionEnter(). If you simply want an enemy projectile to take away some health, use OnTriggerEnter(). In the code, you can customize numerous possibilities for all your game design thoughts.