#5 OnTriggerStay Method. Detecting Objects inside a region.

Introduction

In this article we are going to study how to use the OnTriggerStay method to detect the character in a certain region and apply actions when that happens. Specifically, we’re going to make the damage and health regeneration stations affect the character.

Go to the project’s main page

Procedure

We are going to use the “OnTriggerStay” station, which consists of two metal-detector-shaped devices, one red and one green, whose function is to affect our health system. When entering the red station our health should decrease and with the green station it should increase.

3d models similar to metal detector. In the center a sign that says "OnTriggerStay Station".
Fig. 1: GameDevLab OnTriggerStay Station.

All these objects are inside the empty GameObject “#5 OnTriggerStay“. We have a regeneration station (HealthStation) and a damage station (DamageStation).

the "GameDevLab" scene hierarchy of a Unity3D project.
Fig. 2: Hierarchy of the scene. All elements of the station are in the GameObject #5 OnTriggerStay.

Both HealthStation and DamageStation are assigned the same script, “OnTriggerStayAction“.

component script "OnTriggerStay Action" displayed in inspector in Unity3D
Fig. 3: Script asignado a los GameObjects “HealthStation” y “DamageStation”.

In the inspector we can modify the amount of health that gives or takes away through the float “amountHealth” and we can choose the type of station 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 station does damage or regenerates.

In order for the stations to be able to do their work, it is necessary for the player to have some kind of health system to affect. This is already solved in the GameDevLab. The GameObject FirstPersonPlayer is assigned a script called “HealthSimple” that has the necessary variables and methods to provide the player with a simple health system.

component script "HealthSimple" displayed in inspector in Unity3D
Fig. 5: Script asignado al GameObject FirstPersonPlayer.

Resolution

When opening the OnTriggerStayAction script for the first time we find what you see in figure 6. There is a defined region between comments where it is suggested to redefine the OnTriggerStay method.

c# script in unity
Fig. 6: Script OnTriggerStayAction sin completar.

How does the OnTriggerStay method work?

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

Runs automatically if a GameObject with Collider enters the region defined by another Collider who is in Trigger Mode. In addition, at least one of the two GameObjects must have a RigidBody component assigned to it.

Fig. 7: As long as the colliders do not touch, the OnTriggerStay method does not run.

Fig. 8: Colliders are playing, so the OnTriggerStay method is executed.

Figure 9 shows the resolution for the exercise.

Fig. 9: OnTriggerStayAction script resolution.

We redefine the OnTriggerStay method that receives as parameter the Collider that has come in contact with the trigger. We give the name “col” to the reference of this Collider.

What we have to do is first determine if the GameObject whose Collider came into contact has a health system to affect, we do this by getting the reference of the HealthSimple component that can be assigned to this GameObject.

If the GameObject has health system, we will have a component HealthSimple, otherwise our variable will have the value null. We use this as a condition in the if statement.

If the condition is true we execute the playerIsUnderTheStation method and pass as parameter the HealthSimple reference found in health.

With this we have solved the problem of video, from that point the component HealthSimple will handle 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 value after a few seconds in the damage station.

Upon entering the damage station our health begins to decline, if it reaches zero value the scene is restarted.

Upon entering the regeneration station, health begins to increase until the maximum value is reached.

room with several 3d models in the center you see a sign that says "don't forget to subscribe".
Fig. 11: Health value after a few seconds at the regeneration station.

Conclusion

The objective of this article and corresponding video is to study an application of the OnTriggerStay method, to understand how it works and then be able to apply it to any situation in which it may be useful.

There are many situations in which this method could serve, for example modeling the behavior of a pressure plate, make us affect an opposite force to gravity that makes us levitate if we are in a certain region. In short, this method is a useful tool that we have to understand.

It is not the point of this article to study the health system, but it is interesting to note that the stations are going to act on any GameObject that has the HealthSimple script, regardless of the player. This suggests that in our game we could have a single script that manages the health of any living being in our game and has properties common to all, as well as unique properties for each being. Creating this kind of solutions is what I find most interesting

Scroll to Top
Secured By miniOrange