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.
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.
All these objects are inside the empty GameObject “#5 OnTriggerStay“. We have a regeneration station (HealthStation) and a damage station (DamageStation).
Both HealthStation and DamageStation are assigned the same script, “OnTriggerStayAction“.
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.
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.
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.
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.
Figure 9 shows the resolution for the exercise.
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.
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.
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