Trigger Colliders

Simon Pham
3 min readMay 2, 2024

--

In today’s blog post, let’s discuss trigger colliders in Unity.

What is a trigger collider?

Trigger colliders are special types of colliders in Unity that don’t physically interact with other objects in terms of physics simulation. Instead, they’re used to detect when other objects enter or exit a specific area in the game world. For example, you can use trigger colliders to detect when the player reaches certain areas on the map or when they go through a door, etc.

Trigger collider demonstration

Setting up our scene

I’ve set up a simple scene with three barrels and a container.

Our objective is to create a trigger collider that detects when the barrels come into contact with the container. When this occurs, we want to change the emissive color of the barrels.

Preparing the assets

In order to detect collision between two GameObjects, both must contain a Collider component, one must have Collider.IsTrigger enabled and either of them contains a Rigidbody.

Preparing the barrels

For each barrel, we need to add a collider and a Rigidbody and ensure that we have the Use Gravity property checked.

Preparing the container

For the container, I’m going to create a child GameObject called Trigger and add a box collider to it.

Ensure that you have the Is Trigger property checked.

Scripting

Next, let’s create a script called Trigger and attach it to the Trigger GameObject. In this script, we’re going to use the OnTriggerEnter method to detect the collision and change the color of the barrels’ emission property.

using UnityEngine;

public class Trigger : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
other.gameObject.GetComponent<Renderer>().material.SetColor("_EmissionColor", Color.green * 10);
}
}

And here’s the result:

--

--