Join Alan Thorn for an in-depth discussion in this video Triggers and events, part of Cert Prep: Unity Scripting and Publishing.
- [Instructor] In this chapter, we'll consider a range of common scripting tasks and we'll begin in this movie by considering the if statement and how we can use that practically during gameplay. To demonstrate this, I've opened up the scene in front of us here which is included in the exercise files if you have access to them. It contains a very basic scene with a ground plane and some cubes, as well as a player character being followed by a chase camera.
So for example, if I press play on the toolbar, I can use the W A S and D keys on the keyboard to move the character around and you can see that here inside the game tab. So I can hold down the W key to make the character run forwards here. In addition to this, I've created a health script. If I double-click that to open it inside MonoDevelop, you can see that we have a health script that really is pretty simple. It simply contains a public float variable to maintain all the health points for our character and I've dragged and dropped this script onto the third person controller and inside the object inspector you can see that script has been attached here under the health script and this player character has 100 health points when the level begins.
I want to create a kind of danger zone inside the level that is the space between these two cubes here and if the player runs into it, theirhealth is simply going to be reduced as soon as the player enters this region. And I want to demonstrate how we can do this by using an if statement. But in addition to the if statement, we're going to see a whole range of other features inside Unity such as how we can handle triggers and on trigger events.
So let's get started by creating this setup. So first of all, I've defined that there's a region between these two cubes that effectively can damage the player. And to approximate that region inside Unity, I'm going to be using a trigger volume. To do this, I'm going to create a new and empty object by choosing GameObject, Create Empty to add a new empty object to the scene, and I'm going to maneuver this object into position here so that I can begin to mark out the region. I'm going to bring down the empty to somewhere around about the center point between the two cubes and with this object selected, I am simply going to rename this to killzone because when the player enters this region, they're going to be damaged.
If I'm selecting the object killzone, I'm going to choose component, physics, and then choose box collider to attach a box collider component to this object. By default, this is set to a very small size, but I can tweak the size properties here inside the box collider component to adjust the size of this box. So by clicking and dragging the x field, I can begin to mark out the depth of this object. I'm also going to do the same for the z axis to kind of mark out this region here, and the same thing for the y field too to add some height to this.
Now by default, this acts as a collider, that is, a solid object, meaning that it will actually prevent the player from entering this object. But we can easily fix that by moving to the box collider component and enabling the boolean here that says is trigger. So I'm going to enable the is trigger property. I'm going to press play on the toolbar, and when I do this I can still maneuver the character around and I should be able to make the character run through the trigger volume like so.
Notice that in running through right now the health still remains at 100 points. But here is where we need to create the script file that is going to change this. So I'm going to right-click here inside the project panel, choose create and then choose C# script and I'm simply going to name this script killzone and press enter on the keyboard and I'm going to drag and drop that onto the killzone object. When I select the killzone object here inside the hierarchy panel, you can see from the object inspector here that the killzone script has been attached.
I'm going to double-click the killzone script to open that here inside MonoDevelop and I'm going to delete both the update and the start functions from this class as we really don't need them. Instead, I'm going to write the following function here which is called OnTriggerEnter and it's actually written like this with an argument inside the function. We'll be revisiting the idea of arguments a bit later on but effectively they are inputs that come into the function and they tell us a little bit in this case about the object that entered the trigger.
So in our case, this function is going to happen when an object enters the trigger volume. We've attached this to the trigger inside the scene and there are different times during gameplay when objects might pass through and enter into the trigger and we want to respond to that event. That effectively is what OnTriggerEnter allows us to do, an object enters the trigger and effectively we want to respond in a particular way. Now in the case of the player character, when the player enters the trigger, we want to reduce their health.
So how can we do this? Well effectively when the player enters the trigger, the collider object here is going to stand in for the player character. It's going to represent the player character that entered the trigger. More specifically, it's going to represent the collider component attached to the player because actually in terms of physics, that's the thing that's going to enter the trigger. So what I want to do first of all is I want to get access to the player object. So to do that, I can choose GameObject, PlayerObject equals Col.gameObject.
This will get the game object that the collider which has entered the trigger is attached to and I'm going to store a local copy of it inside the player object variable. Notice that the data type for this object is game object. So this effectively is going to represent the player that enters the collider. Now, having done this, I want to reduce the player's health and we know already that the health value is stored inside the health script here. So I need to get access to the health script and I can do that by using the GetComponent function.
So I'm going to create a new local variable and call this for example PlayerHealth and it's going to equal the player object that we have just retrieved. And I'm going to call GetComponent and I want to get the health component that is attached to the player object. Now having retrieved the health component, I need to reduce the health points of the player object which is contained inside the health component. So in the next line, I simply want to write player health and then to access the health points I need to type the period symbol and then simply type HealthPoints, and then what should these health points equal? Well effectively the health point should equal the value that they currently are minus the damage value.
So I'm going to say the health points should equal the player health points, whatever they currently are, minus the damage value. In this case I'm going to say that the trigger volume is going to be worth 10 points of damage. So every time the player enters the trigger volume, they're going to be damaged by 10 points. So I'm simply going to say 10 points worth of damage subtracted from the health points here. And that looks pretty good. Now there's only one problem with this piece of code and that is it depends on the player entering the trigger volume.
It could be that there are other objects in the scene. For example, enemy characters or weapons or maybe even vehicles that could potentially enter the trigger volume. Now, if those objects enter the trigger volume, they may not have a health script attached. This piece of code depends on the fact that it will be the player that's entering the trigger. So in the first line, I'm going to want to check to make sure that the object that has entered the trigger is in fact the player character and not some other kind of object.
And to verify this, I can use an if statement. Let me show you how this works. I'm going to type the statement if, and effectively I want to have opening and closing parenthesis and inside here I want to put the condition that I want to check for. Now in this case we know that the collider object has entered the trigger so I'm going to select the collider object and choose dot, and then I'm going to type CompareTag and I want to compare what tag is assigned to this object. In fact, I want to check that the tag that is being assigned to the object is the player tag.
Now I can prefix this statement, CompareTag, with the exclamation mark, and effectively what this if statement is saying is if the object that has entered trigger does not, because the exclamation mark means not, it means the negative, so if the object that has entered the trigger is not tagged as player, then really I'm not interested in processing this object any further because the object that has entered the trigger is not the player.
It doesn't have health points. There's no point in reducing its health, it's not the kind of thing that has health. So I'm simply going to say exit the function at this point and don't bother with the remaining lines. I'm going to press command S on the keyboard to save this code and minimize MonoDevelop here. I'm going to let Unity compile the code that I've attached here. And notice that it's still attached to the killzone, the kill object. I'm going to now select the player character here inside the hierarchy panel and scroll down to the health script just so that when the game is running, I can keep an eye on this component and the value of the health points.
In fact, I'm just going to scroll up just quickly here to ensure that the player character is in fact tagged as player and indeed he is. This is critically important because as we enter the trigger volume it's going to compare that the player tag has been assigned to this object. I'm going to press play on the toolbar to move the player character around and then pass through these two cube objects and you'll see that in doing it as soon I've entered the trigger volume, you can see from the health script that the health points has been reduced by 10 points.
So in fact, on entering the trigger volume, we've checked to make sure that it was the player that entered, and if it was, we reduced the players health by 10 points. This is the power of if statements and trigger volumes.
- Creating a script file
- Configuring a code editor
- Gameplay mechanics
- Object spawning and deletion
- Getting started with coroutines
- Launching and pausing coroutines
- Cloud Build