Skip to Content
Unity Tutorial: Collider Distance Trigger

Write a custom C# script to trigger events when an object is within a certain distance to a collider.

Date 29 Nov 2024
Share
Requirements

Allow a technical artist to trigger events when an object is in proximity to a specific collider.

Separate events should fire when the object enters and exits proximity.

A blank URP project  in Unity 2022.3.36f1.

For this example, we're going to change the color of a mesh when a player object is near it

The cube represents the collider/object, and the sphere represents the player.

Screenshot 2024 11 29 162814
Script

Create a new C# Script called ProximityTrigger, and attach it to the player object (sphere).

This script will have one or more references to colliders it needs to "watch out" for, and every frame calculate the closest distance to them.

We want the designer to be able to add any collider they want to track, and also change the distance that causes the events to occur.

Screenshot 2024 11 292

Add the following parameters to the ProximityTrigger script:

  • public Collider : The collider reference to watch.
  • public float : The distance that causes proximity triggers.
  • public UnityEvent (2) : One event to fire when the object enters proximity, and another to fire when the object exits proximity.
  • private bool : A flag to keep track of whether or not the object is currently in proximity to the collider.
Screenshot 2024 11 293

Create a new method called CheckDistance(), and call it from the Update() method.

This will make it so every frame the isInProximity flag will be updated based on the distance to the collider, and enter/exit events are fired when that flag changes.

Screenshot 2024 11 294
Doing the Thing

The designer can add reference to any other scripts or components directly in the ProximityTrigger inspector's enter and exit Unity Events. This can do many things by using Unity's component API, such as enabling/disabling objects, or playing animator states (which is what we're going to do).

For this example, we created a new AnimatorController for the wall object in the Project View, then dragged it onto the wall object to automatically attach an Animator component.

Screenshot 2024 11 296

The animator has the following states:

  1. off : Animation to play when the object isn't in proximity.
  2. enter-proximity : Animation to play when entering proximity.
  3. exit-proximity : Animation to play when exiting proximity.
  4. on : Animation to play when the object is in proximity.

A transition exists from exit-proximity to off with an exit time of 1, and another from enter-proximity to on.

The enter/exit-proximity animations modify the object's material color, in this case the URP material Base Color property.

The on/off animations are single-frame, holding the object's material color at a single value.

All animations are set to loop.

Mesh animation

The ProximityTrigger component is attached to the Sphere (player). We added references to the Wall's Animator in the enter and exit events, and set the distance to 2.

Now when the sphere is moved to within 2.0 units of the wall, the transition animations play.

Screenshot 2024 11 297
1e3c6c9ac67c492a60615eaeda4c9a38
Conclusion

Unity Event triggers are a great way to give designers freedom to connect properties and behaviors between objects in a scene or prefab.

Download the Unity Package of this example and import it into a blank URP project to test it for yourself!

If you have any questions or suggestions on this tutorial or any others, feel free to send us a message using the Contact Form.