#6 OnTriggerEnter and OnTriggerExit Methods. Detecting objects in Unity

By GameDevTraum

Introduction

In this article we are going to study how to use the OnTriggerEnter (Collider c) and OnTriggerExit (Collider c) methods to detect the player in a given region and apply actions when that happens. Specifically we are going to make the GameDevLab door open when the player is in front of it and close when it moves away.

Go to the project’s main page

Before I start I invite you to watch the video I made to summarize this article. Make sure to activate the english subtitles


Procedure

We are going to use the “OnTriggerEnter/Exit” station, which consists of a portal that leads to a small room that for now has nothing special.

Initially the door is closed, our goal is to detect the character when he approaches and open the door to let him in and away from the portal the door must close.

project unity3d, model 3d of door made in blender, laboratory of programming
Fig. 1: GameDevLab OnTriggerEnter/Exit Station.

All elements of the station are contained in the GameObject “#6 OnTriggerEnter/Exit”.

Fig. 2: Hierarchy of the GameDevLab. Let’s work with the GameObject “#6 OnTriggerEnter/Exit”.

This GameObject is assigned the script “OnTriggerEnterExit” which is the one we have to complete on this occasion.

Fig. 3: Components of the GameObject “#6 OnTriggerEnter/Exit”. We have assigned the Script to complete in this exercise and a Box Collider in trigger mode.

In addition to a Mesh Collider so that the character does not cross the structure, it also has a Box Collider in Trigger mode, as seen in figures 3 and 4.

project unity3d, model 3d of door made in blender, laboratory of programming, collider
Fig. 4: Box Collider in trigger mode that we will use to detect the character.

Let’s use this Collider to solve the exercise.

project unity3d, model 3d of door made in blender, laboratory of programming
Fig. 5: Opening the door once we solve the exercise.

project unity3d, model 3d of door made in blender, laboratory of programming
Fig. 6: View from inside the room.

If you want to know more about Colliders I invite you to watch this video I made for the series “My First Game in Unity”.

Resolution

When we open the OnTriggerEnterExit Script for the first time we find two methods, the openDoor() method that will open the door and the closeDoor() method that will close it.


Article: What is a method in programming?

In addition, the region where the redefinition of the OnTriggerEnter(Collider c) and OnTriggerExit(Collider c) methods is suggested is indicated with green comments.

Fig. 7: Script OnTriggerEnterExit sin completar.

We’re not going to worry about how the door opens and closes because that’s already solved in GameDevLab.

How the OnTriggerEnter(Collider c) and OnTriggerExit(Collider c) methods work

In the previous article we saw how the OnTriggerStay method worked and we used it to make the damage and regeneration stations affect the player’s health.

Article: OnTriggerStay Method

For the OnTriggerEnter and OnTriggerExit methods the same requirements are needed, what changes is the moment they are executed.

Fig. 8: Components required for the operation of the methods.

Fig. 9: The OnTriggerEnter method is automatically executed on the first frame in which a collider enters the trigger.

OnTriggerEnter runs the moment the player enters the trigger for the first time, i.e. the previous frame of the game was outside the trigger and the current frame is inside.

Fig. 10: The OnTriggerExit method is automatically executed on the first frame in which a collider exits the trigger it had previously entered.

On the other hand, the OnTriggerExit method is executed at the moment in which the player leaves the trigger, that is to say in the previous frame of the game it was inside the trigger and in the current frame it is outside.

Fig. 11: Overview of the operation of the methods.

With this in mind, what we can do is open the door in the OnTriggerEnter method and close it in the OnTriggerExit method.

In addition we are going to verify that the tag of the Collider that entered in the trigger is equal to “Player”, of that form we know that it is about the player. The following figure shows the resolution.

Fig. 12: Resolution of the exercise within the region defined by the green comments.

Conclusion

The OnTriggerEnter and OnTriggerEnter methods allow us to detect GameObjects that enter a certain region, this has a number of applications, for example activate kinematics, mechanisms, allow us to perform certain actions only when we are in a certain region, and so on.

Unlike the OnTriggerStay method, these methods are run only once when one of the above situations occurs.