Journal 10 — Collisions in Unity, OnTriggerEnter and OnCollisionEnter

Chris Nielsen
4 min readApr 27, 2021

Consider the following scenarios in games:

— Your character sees a glowing portal, and you need to step through to teleport.

— Your character sees a floor plate, which triggers a nearby door to open.

— You are driving a car game, and your car collides with another player or you crash into a wall. You want to see the effects of the crash and your car flying off in another direction.

All these scenarios can be handled through Unity’s OnTriggerEnter and OnCollisionEnter methods. Also refer to Unity documentation for a Colliders overview.

In general, OnTriggerEnter can be used when no Physics need to be applied. OnCollisionEnter would typically be used in the example above, where two Rigidbody objects collide and Physics are applied.

I’ll show you an example of a door that can be opened by triggering a floor plate.

  1. Create some basic shapes in a scene that indicate a wall, a door, a floor plate trigger, and the player.
  2. Create a C# script and attach it to the floor plate trigger.
  3. When the player touches the trigger, we can send a command to make the door disappear by translating its position below the floor.
  4. Use the function OnTriggerEnter which checks for a collider. In this example we can check for the game object tagged “Player” in the Inspector.
Unity Playmode — Trigger Floor Plate to Open Door

To spice things up, you could add particle effects, sound, and animate the door rotating or sliding out of the way.

Another example is to create two teleport floor plates that can transport the Player.

  1. Use the same game objects as the previous example, and copy another floor plate teleport.
  2. Add two new C# scripts called Teleport1 and Teleport2 and attach one to each floor plate.
  3. Add a public GameObject for the Player and a public GameObject for the destination teleporter.
  4. I have included a Vector3 offset, so that once the Player is teleported, his new position is shifted away from the teleport floor plate, so the Player does not immediately trigger the landed on floor plate, where you could end up in an endless loop of teleporting back and forth.
Unity C# Teleport Player using OnTriggerEnter
Unity Playmode — Teleport Player in Action

Similar to the first example, you could add particle effects, animations, and sound effects.

A current project I am working on is building a 2D topdown shooter. The basic gameplay includes instantiating laser prefabs and shooting them from the Player to hit the spawning enemies.

In this example, we use OnTriggerEnter to see what the enemy ran into. If the enemy hits a laser prefab, both the laser prefab and the enemy are destroyed.

If the enemy collides with the Player, then the Player takes some damage, and the enemy is destroyed. Similar to the “Player” tag, you can create your own tags for “Enemy” and “Laser”, and whatever other potential collisions you might want to check.

Unity C# OnTriggerEnter for checking Laser, Enemy, and Player collisions
Unity Playmode-Player lives lost, enemy and lasers destroyed

Let’s try another experiment with collisions, and see how OnCollisionEnter could be used.

In the previous Journal, we used a sphere, cube, and plane setup, and had the sphere and cube crash into one another before hitting the ground.

  1. Change the sphere C# script a bit from OnTriggerEnter to OnCollisionEnter
  2. Add a private Rigidbody variable and use GetComponent to get the sphere’s Rigidbody.
  3. From the OnCollisionEnter method, we can get contact information at the point of collision. We can show a Debug.DrawRay in the Scene view at each collision (does not work in Game view).
  4. We can add a rigidbody force of a selected magnitude normal to the contact point.
  5. Adjust the objects in the Scene view (e.g., tilt the floor, change the positions of the sphere and cube) for maximum effect.
Unity Playmode — See collision results

A slight variation is to add an initial force to the sphere in a sideways direction, so we can see different angles of collisions.

Unity C# — Add a sideways force to sphere
Unity Playmode — sideways force applied at Start

I appreciate you stopping by.

Thanks!

--

--

Chris Nielsen

An Engineering Manager consultant who is seeking additional skills using Unity 3D for game and application development.