#6 OnTriggerEnter and OnTriggerExit Methods. Detecting objects in Unity

IMPORTANT UPDATE

This arti­cle is part of an old­er video series, I recent­ly upload a Uni­ty pro­to­type to help under­stand­ing how the OnTrig­ger sys­tem works, how to con­fig­ure the objects from the scene and how the func­tions are called. Please check the oth­er arti­cle to see the updat­ed infor­ma­tion and down­load the Uni­ty Package.

ABOUT THESE VIDEOS

The fol­low­ing videos show how to con­fig­ure the ele­ments in Uni­ty in order to detect objects enter­ing into anoth­er object, the video on the left describes all the GameOb­jects and com­po­nents in the Uni­ty Edi­tor in order to con­fig­ure col­lid­ers to detect anoth­er col­lid­ers. In the video on the right we ana­lyze the func­tions from the Script that are called when a col­lid­er enters anoth­er col­lid­er mark as trigger.

HERE MY UNITY PLAYLIST
ūüĎáūüŹĹ

GO TO THE NEW ARTICLE

HERE CONTINUES THE OLD ARTICLE

Introduction

In this arti¬≠cle we are going to study how to use the OnTrig¬≠ger¬≠Enter (Col¬≠lid¬≠er c) and OnTrig¬≠gerEx¬≠it (Col¬≠lid¬≠er c) meth¬≠ods to detect the play¬≠er in a giv¬≠en region and apply actions when that hap¬≠pens. Specif¬≠i¬≠cal¬≠ly we are going to make the GameDe¬≠vLab door open when the play¬≠er is in front of it and close when it moves away.

Go to the project‚Äôs main page


Procedure

We are going to use the "OnTriggerEnter/Exit" sta¬≠tion, which con¬≠sists of a por¬≠tal that leads to a small room that for now has noth¬≠ing special. 

Ini¬≠tial¬≠ly the door is closed, our goal is to detect the char¬≠ac¬≠ter when he approach¬≠es and open the door to let him in and away from the por¬≠tal the door must close.

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

All ele­ments of the sta­tion are con­tained in the GameOb­ject "#6 OnTriggerEnter/Exit".

Fig. 2: Hier­ar­chy of the GameDe­vLab. Let's work with the GameOb­ject "#6 OnTriggerEnter/Exit".

This GameOb­ject is assigned the script "OnTrig­ger­EnterEx­it" which is the one we have to com­plete on this occasion.

Fig. 3: Com¬≠po¬≠nents of the GameOb¬≠ject "#6 OnTriggerEnter/Exit". We have assigned the Script to com¬≠plete in this exer¬≠cise and a Box Col¬≠lid¬≠er in trig¬≠ger mode.

In addi¬≠tion to a Mesh Col¬≠lid¬≠er so that the char¬≠ac¬≠ter does not cross the struc¬≠ture, it also has a Box Col¬≠lid¬≠er in Trig¬≠ger mode, as seen in fig¬≠ures 3 and 4.

project unity3d, model 3d of door made in blender, laboratory of programming, collider
Fig. 4: Box Col­lid­er in trig­ger mode that we will use to detect the character.

Let's use this Col­lid­er to solve the exercise.

project unity3d, model 3d of door made in blender, laboratory of programming
Fig. 5: Open­ing 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 Col­lid­ers I invite you to watch this video I made for the series "My First Game in Uni­ty".

CHECK OUT THIS CRAZY EXPERIMENT

Resolution

When we open the OnTrig¬≠ger¬≠EnterEx¬≠it Script for the first time we find two meth¬≠ods, the open¬≠Door() method that will open the door and the close¬≠Door() method that will close it.


Arti­cle: What is a method in programming?

In addi­tion, the region where the rede­f­i­n­i­tion of the OnTriggerEnter(Collider c) and OnTriggerExit(Collider c) meth­ods is sug­gest­ed is indi­cat­ed with green comments.

Fig. 7: Script OnTrig­ger­EnterEx­it sin completar.

We're not going to wor­ry about how the door opens and clos­es because that's already solved in GameDevLab.

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

In the pre­vi­ous arti­cle we saw how the OnTrig­ger­Stay method worked and we used it to make the dam­age and regen­er­a­tion sta­tions affect the player's health.

Arti­cle: OnTrig­ger­Stay Method

For the OnTrig­ger­Enter and OnTrig­gerEx­it meth­ods the same require­ments are need­ed, what changes is the moment they are executed.

Fig. 8: Com­po­nents required for the oper­a­tion of the methods.

Fig. 9: The OnTrig­ger­Enter method is auto­mat­i­cal­ly exe­cut­ed on the first frame in which a col­lid­er enters the trigger.

OnTrig­ger­Enter runs the moment the play­er enters the trig­ger for the first time, i.e. the pre­vi­ous frame of the game was out­side the trig­ger and the cur­rent frame is inside.

Fig. 10: The OnTrig¬≠gerEx¬≠it method is auto¬≠mat¬≠i¬≠cal¬≠ly exe¬≠cut¬≠ed on the first frame in which a col¬≠lid¬≠er exits the trig¬≠ger it had pre¬≠vi¬≠ous¬≠ly entered. 

On the oth¬≠er hand, the OnTrig¬≠gerEx¬≠it method is exe¬≠cut¬≠ed at the moment in which the play¬≠er leaves the trig¬≠ger, that is to say in the pre¬≠vi¬≠ous frame of the game it was inside the trig¬≠ger and in the cur¬≠rent frame it is outside. 

Fig. 11: Overview of the oper­a­tion of the methods.

With this in mind, what we can do is open the door in the OnTrig¬≠ger¬≠Enter method and close it in the OnTrig¬≠gerEx¬≠it method. 

In addi­tion we are going to ver­i­fy that the tag of the Col­lid­er that entered in the trig­ger is equal to "Play­er", of that form we know that it is about the play­er. The fol­low­ing fig­ure shows the resolution.

Fig. 12: Res­o­lu­tion of the exer­cise with­in the region defined by the green comments.

Conclusion

The OnTrig¬≠ger¬≠Enter and OnTrig¬≠ger¬≠Enter meth¬≠ods allow us to detect GameOb¬≠jects that enter a cer¬≠tain region, this has a num¬≠ber of appli¬≠ca¬≠tions, for exam¬≠ple acti¬≠vate kine¬≠mat¬≠ics, mech¬≠a¬≠nisms, allow us to per¬≠form cer¬≠tain actions only when we are in a cer¬≠tain region, and so on.

Unlike the OnTrig­ger­Stay method, these meth­ods are run only once when one of the above sit­u­a­tions occurs.

YouTube
Scroll to Top
Secured By miniOrange