How does OnTriggerEnter works in Unity? Detecting objects entering an area

Introduction

In this arti­cle we are going to see how the OnTrig­ger­Enter method works in Uni­ty. This func­tion will allow us to know when the Col­lid­er of a cer­tain GameOb­ject has enter anoth­er Col­lid­er that is spe­cial­ly con­fig­ured to be able to detect.

Dear read­er

🍷🧐

OnTrig­ger­Enter and OnTrig­gerEx­it are very sim­i­lar meth­ods but it changes the moment in which the exe­cu­tion of these meth­ods hap­pens, know­ing exact­ly these moments will allow us to build solutions. 

Here are two videos that ana­lyze a pro­to­type using OnTrig­ger­Enter and OnTrig­gerEx­it to exe­cute actions when an object enters a trig­ger Collider.

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 trig­ger. You can down­load this Uni­ty pack­age here.

MY COMPLETE UNITY PLAYLIST 👇🏽

Download Unity Package

Below you can down­load the Uni­ty pack­age that when import­ed into your engine, you will recov­er the Assets we use and you will be able to test it on your own computer.

🎁 Uni­ty Pack­age: Object detec­tion and Trig­ger Events in Unity

ABOUT THIS SOLUTION

  • By click­ing in Down­load you will get a file with "Uni­ty Pack­age" exten­sion. Drag and drop that file into Uni­ty to import it.
  • When you import this Uni­ty Pack­age you will find a fold­er with 1 scene, 2 scripts and some extras.
  • Inside the scenes you'll find infor­ma­tion about how this solu­tions works.

OnTriggerEnter Method in Unity

The OnTrig­ger­Enter method is defined in the MonoBe­hav­iour class. That is to say that any new Script by default will have it by inheritance.

Sev­er­al con­di­tions must be met for OnTrig­ger­Enter to run. 

The first is that there must be two GameOb­jects with Col­lid­ers involved, one of the Col­lid­ers must be in Trig­ger mode, for this you must check the box "Is Trig­ger" in the inspector.

In addi­tion, at least one of the GameOb­jects involved must be assigned a Rigid­Body com­po­nent, because OnTrig­ger­Enter is relat­ed to the phys­i­cal part of the engine.

MOST SEARCHED VIDEOS FROM MY CHANNEL

ABOUT UNITY

ABOUT BLENDER

Declare OnTriggerEnter

The fact that it is defined in the MonoBe­hav­iour class and a new default Script does not mean that we will auto­mat­i­cal­ly have OnTrig­ger­Enter in our Script, we must declare it and we do it in the fol­low­ing way:


private void OnTriggerEnter(Collider c){

//Acciones a realizar cuando se detecta una entrada al Trigger.

}

If the con­di­tions are met, this method will run auto­mat­i­cal­ly, so inside we write every­thing that needs to be done when that happens.

The para­me­ter "Col­lid­er c" that receives the method, is the Col­lid­er of the GameOb­ject that came into con­tact with the Trig­ger. We can use this para­me­ter to do many things, for exam­ple get the GameOb­ject that has assigned that col­lid­er, we do this sim­ply with:

c.gameObject

From there we can read any oth­er com­po­nent assigned to GameOb­ject, its tag, its lay­er, mod­i­fy what we need and we are allowed to mod­i­fy, exe­cute meth­ods defined in Scripts that may be assigned and many more actions.

To learn more about pro­gram­ming func­tions with para­me­ters I invite you to read this arti­cle.

Conclusion

The OnTrig­ger­Enter method allows us to detect when two GameOb­jects over­lap their Col­lid­ers, so we can apply all the nec­es­sary actions.

OnTrig­ger­Enter runs auto­mat­i­cal­ly, for this to hap­pen there must be some con­di­tions, the first is that there must be two GameOb­jects with Col­lid­ers involved and one of them in Trig­ger mode and the sec­ond is that at least one of the GameOb­jects must have a Rigid­Body com­po­nent assigned.

Scroll to Top
Secured By miniOrange