#6 OnTriggerEnter and OnTriggerExit Methods. Detecting objects in Unity
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.
Before I start I invite you to watch the video I made to summarize this article. Make sure to activate the english subtitles
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.
All elements of the station are contained in the GameObject “#6 OnTriggerEnter/Exit”.
This GameObject is assigned the script “OnTriggerEnterExit” which is the one we have to complete on this occasion.
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.
Let’s use this Collider to solve the exercise.
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”.
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.
In addition, the region where the redefinition of the OnTriggerEnter(Collider c) and OnTriggerExit(Collider c) methods is suggested is indicated with green comments.
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.
For the OnTriggerEnter and OnTriggerExit methods the same requirements are needed, what changes is the moment they are executed.
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.
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.
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.
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.