Understanding OnTrigger methods in Unity

Let’s talk about OnTrigger methods in Unity! Do you know when they are invoked or for what you can use them? Let’s see in this post! 🔥

In one of the recent posts, we spoke about OnCollision methods and the fact that Unity has two separate physics engines.

Of course, like with collisions, there are different trigger methods for the 2D and the 3D engines. For 3D there are OnTriggerEnter(), OnTriggerStay() and OnTriggerExit() methods. For 2D there are almost the same OnTriggerEnter2D(), OnTriggerStay2D() and OnTriggerExit2D() methods.

When they are called?

There are more similarities to the colliders also when it comes to the invoking of these methods. To make Unity invoke OnTrigger method, two objects have to have Collider attached to them. At least one of these objects has to have Collider turned into a Trigger with simple checkbox. The same is true with the Rigidbody, at least one object has to have it. You can also make it in 2D, but you have to use 2D components.

And again, 2D and 3D physics engines don’t interact with each other so don’t mix them together. 😬

Let’s start with OnTriggerEnter() method. As the name suggests, it will be invoked when an object with collider enters the trigger. It can also be another trigger as long as one of these objects will have a Rigidbody component.

Similarly to the colliders, OnTriggerStay() method will be called each frame as long as an object will be inside a trigger.

And at the end, when an object will leave the trigger, Unity will invoke OnTriggerExit() method.

Fun fact is that that these functions will be invoked on both objects – Trigger and object which entered the Trigger.

When to use each of them?

So the obvious question is when or for what should you use each of them?

Starting again from OnTriggerEnter(), you can use this method to trigger some actions on surrounding elements. These could be doors or traps. You can even use it to change the music when the player enters a new location. Another example could be AI, where you can use this method to trigger a chase after a player.

OnTriggerStay() could also be used for music when the longer you stay inside the music will start changing. It also can be used for damage zones so that every few seconds player will get damage.
AI can use it also, especially when it will try to run away from the player if he gets too close.

And the last on our list is OnTriggerExit(). This method can be used for the same examples as in previous methods, but here you can finish them. Doors will shut, music will stop, and AI will get back to doing other AI things like assimilating any form of life. 🤖

Exercise for you

Use a script from bellow and attach it to a few objects on the scene. See when they are called and for how long.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TriggerTester : MonoBehaviour
{
    private void OnTriggerEnter(Collider other)
    {
        Debug.LogFormat("{0} trigger enter: {1}", this, other);
    }

    private void OnTriggerStay(Collider other)
    {
        Debug.LogFormat("{0} trigger stay: {1}", this, other);
    }

    private void OnTriggerExit(Collider other)
    {
        Debug.LogFormat("{0} trigger exit: {1}", this, other);
    }
}

After that, let me know what you found out in the comment section below! 😉

And if you want to get notified on future content, sign up for the newsletter!

And I hope to see you next time! 🤓

5 1 vote
Article Rating
Subscribe
Notify of
guest
6 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Axel
Axel
1 year ago

Hello Patryk,

when I use the method “OnTriggerExit”, Unity send an error message : “Error CS0106 : The modifier private is not valid for this item.”

Do you know why ?

Thanks for answering me.

Axxl dd

Axel
Axel
1 year ago

I do not believe, i join 2 screen scans,unity_0 and unity_1,. When I try the unity_0 I have the error message, when I try the second one I have no message but it does not work either.

unity_0.png
Axel
Axel
1 year ago

and unity_1

unity_1.png
zayid
zayid
3 months ago

My OnTriggerEnter option is not showing. what to do?

6
0
Would love your thoughts, please comment.x
()
x