#5 OnTriggerStay Method. Detecting Objects inside a region.


In this arti­cle we are going to study how to use the OnTrig­ger­Stay method to detect the char­ac­ter in a cer­tain region and apply actions when that hap­pens. Specif­i­cal­ly, we're going to make the dam­age and health regen­er­a­tion sta­tions affect the character.

Go to the project’s main page





We are going to use the "OnTrig­ger­Stay" sta­tion, which con­sists of two met­al-detec­tor-shaped devices, one red and one green, whose func­tion is to affect our health sys­tem. When enter­ing the red sta­tion our health should decrease and with the green sta­tion it should increase.

3d models similar to metal detector. In the center a sign that says "OnTriggerStay Station".
Fig. 1: GameDe­vLab OnTrig­ger­Stay Station.

All these objects are inside the emp­ty GameOb­ject "#5 OnTrig­ger­Stay". We have a regen­er­a­tion sta­tion (Health­Sta­tion) and a dam­age sta­tion (Dam­ageS­ta­tion).

the "GameDevLab" scene hierarchy of a Unity3D project.
Fig. 2: Hier­ar­chy of the scene. All ele­ments of the sta­tion are in the GameOb­ject #5 OnTriggerStay.

Both Health­Sta­tion and Dam­ageS­ta­tion are assigned the same script, "OnTrig­ger­Stay­Ac­tion".

component script "OnTriggerStay Action" displayed in inspector in Unity3D
Fig. 3: Script asig­na­do a los GameOb­jects "Health­Sta­tion" y "Dam­ageS­ta­tion".

In the inspec­tor we can mod­i­fy the amount of health that gives or takes away through the float "amountHealth" and we can choose the type of sta­tion that is.

component script "OnTriggerStay Action" displayed in inspector in Unity3D. Drop-down menu with health and damage options
Fig. 4: The script allows us to choose whether the sta­tion does dam­age or regenerates.

In order for the sta­tions to be able to do their work, it is nec­es­sary for the play­er to have some kind of health sys­tem to affect. This is already solved in the GameDe­vLab. The GameOb­ject First­Per­son­Play­er is assigned a script called "Health­Sim­ple" that has the nec­es­sary vari­ables and meth­ods to pro­vide the play­er with a sim­ple health system.

component script "HealthSimple" displayed in inspector in Unity3D
Fig. 5: Script asig­na­do al GameOb­ject FirstPersonPlayer.


When open­ing the OnTrig­ger­Stay­Ac­tion script for the first time we find what you see in fig­ure 6. There is a defined region between com­ments where it is sug­gest­ed to rede­fine the OnTrig­ger­Stay method.

c# script in unity
Fig. 6: Script OnTrig­ger­Stay­Ac­tion sin completar.

How does the OnTriggerStay method work?

This method is defined in the MonoBe­hav­iour class, so it will be present by default in any new script we make.

Runs auto­mat­i­cal­ly if a GameOb­ject with Col­lid­er enters the region defined by anoth­er Col­lid­er who is in Trig­ger Mode. In addi­tion, at least one of the two GameOb­jects must have a Rigid­Body com­po­nent assigned to it.

Fig. 7: As long as the col­lid­ers do not touch, the OnTrig­ger­Stay method does not run.

Fig. 8: Col­lid­ers are play­ing, so the OnTrig­ger­Stay method is executed.

Fig­ure 9 shows the res­o­lu­tion for the exercise.

Fig. 9: OnTrig­ger­Stay­Ac­tion script resolution.

We rede­fine the OnTrig­ger­Stay method that receives as para­me­ter the Col­lid­er that has come in con­tact with the trig­ger. We give the name "col" to the ref­er­ence of this Col­lid­er.

What we have to do is first deter­mine if the GameOb­ject whose Col­lid­er came into con­tact has a health sys­tem to affect, we do this by get­ting the ref­er­ence of the Health­Sim­ple com­po­nent that can be assigned to this GameObject.

If the GameOb­ject has health sys­tem, we will have a com­po­nent Health­Sim­ple, oth­er­wise our vari­able will have the val­ue null. We use this as a con­di­tion in the if statement.

If the con­di­tion is true we exe­cute the play­erIsUn­derTheS­ta­tion method and pass as para­me­ter the Health­Sim­ple ref­er­ence found in health.

With this we have solved the prob­lem of video, from that point the com­po­nent Health­Sim­ple will han­dle the issue of health.

room with several 3d models, vending machine, gears, in the middle there is a sign that says "don't forget to subscribe".
Fig. 10: Health val­ue after a few sec­onds in the dam­age station.

Upon enter­ing the dam­age sta­tion our health begins to decline, if it reach­es zero val­ue the scene is restarted.

Upon enter­ing the regen­er­a­tion sta­tion, health begins to increase until the max­i­mum val­ue is reached.

room with several 3d models in the center you see a sign that says "don't forget to subscribe".
Fig. 11: Health val­ue after a few sec­onds at the regen­er­a­tion station.


The objec­tive of this arti­cle and cor­re­spond­ing video is to study an appli­ca­tion of the OnTrig­ger­Stay method, to under­stand how it works and then be able to apply it to any sit­u­a­tion in which it may be useful.

There are many sit­u­a­tions in which this method could serve, for exam­ple mod­el­ing the behav­ior of a pres­sure plate, make us affect an oppo­site force to grav­i­ty that makes us lev­i­tate if we are in a cer­tain region. In short, this method is a use­ful tool that we have to understand.

It is not the point of this arti­cle to study the health sys­tem, but it is inter­est­ing to note that the sta­tions are going to act on any GameOb­ject that has the Health­Sim­ple script, regard­less of the play­er. This sug­gests that in our game we could have a sin­gle script that man­ages the health of any liv­ing being in our game and has prop­er­ties com­mon to all, as well as unique prop­er­ties for each being. Cre­at­ing this kind of solu­tions is what I find most interesting

Scroll to Top
Secured By miniOrange