From the course: Cert Prep: Unity Certified Associate Game Developer Scripting with C#
Triggers and events - Unity Tutorial
From the course: Cert Prep: Unity Certified Associate Game Developer Scripting with C#
Triggers and events
- [Illustrator] In the previous movie, we displayed a countdown, a timed behavior that could display three, two, one go, and then the car could jump into action. And we created a countdown script to do that. In this movie, what we're going to do is to create an area based effect so that as the car enters a checkpoint or reaches the finish line, we're going to display a congratulations message. Now this is a very common type of behavior. If you have the player, character or the viewer enter a particular region of a scene or a level, you want to respond in particular ways. Let's see how we can set that up. So already inside the scene, we have our countdown. If I press play on the toolbar you can see we have three, two, one, and then go and we can move the car into action here. And that's great. Of course the message go remains with us. And actually I want to hide the go message. So I'm going to just select the countdown. We can easily do that by scrolling down to the countdown section, we have three, two, one go. I'm going to increase the message count here so that we have three, two, one, and then here we have go. And then for go here, I'm just going to have some spaces. So now when I press play on the tool bar we get the same countdown as before, we get go and then it's empty and we can finally move the car and the go message is no longer being displayed and that's great. Okay. So now how can we display this area based effect? When the car enters a particular region we want to display a particular message. Well, to do that we're going to be using triggers here inside Unity. Now I'm going to switch back to the scene tab here. We're currently in 2D mode. You may not be in this mode, that's fine. But if you are in this mode just hit 2D at the top here to come out of that. And I want to zoom in on the car itself. So that's the family car. I will double click on the family car to display that inside the scene. So here's the car and I want to display a message when the car enters a particular region. And for test purposes, we're going to perhaps say this region of the track here. When the car enters this region of the track we want to display a new message. Well, to do that, I'm going to create a new empty object inside the scene. I'm going to choose game object, create empty and then move that onto the racetrack. And actually it's a pretty good location. Just move that up here. Maybe somewhere kind of like this too about this location here, that's looking pretty good. I might rotate the object a little bit so that the axes of the object are oriented along with the road. So this is the forward vector here. You can see these are aligned to the road here. The next thing I'm going to do in selecting this object is I'm going to name it here at the top end the inspector to check point. And then move to the inspector and choose, add component. And from the add component menu, I'm going to be adding a box collider component and select that option. Now in selecting the box collider here inside the scene view, when you look at these axes you can see that Unity is drawing a box inside the scene. Well, this box is actually pretty small, but this is going to be the box that the car is going to enter and in response, we are going to do something. So I'm going to increase the size of this box. It's really easy to do that. I can move to the inspector here to the box collider component and for the size field, for the X value just click and drag to increase that. Same thing for the Z field here and also for the Y. I want to create this box to be the kind of thing that the car really so long as it's remaining on the track, it's not the kind of thing that it can easily avoid. And that's looking pretty good. Now that I've created this box collider, the next thing that I want to do is to move to the inspector and make sure that I activate the is triggered checkbox. If I don't activate that checkbox, Unity is going to think of this collider as an invisible wall. The car is going to bump into this wall and it won't be able to pass through. What I actually want to do is for half the car passing through this as though the box were not there at all. We're just using this to mark out a volume inside the level where we can detect when the car enters. Great. So we have this checkpoint in place. The next thing we want to do is we want to check when the car enters the checkbox. Now, to start setting that up, I'm going to select the car itself. And from the inspector, you can see for the tag field that the family car currently has no tag assigned. This is going to be important. I'm going to click on the dropdown and assign this the player tag to indicate that this object is the object being controlled by the player. The reason this is important is because when I come to detect what has entered the checkpoint, I want to figure out what kind of object has entered it. We could be creating a game where there are multiple objects inside the level that move around. And we're only interested in cases where the player enters this check box as opposed to other objects and we're using the tags to distinguish the objects. The next thing I want to do is to move to the project panel here, inside the script's folder and right click and choose, create and choose C-sharp script. I'm going to call this script check point and press enter on the keyboard. And then I'll open that script inside Visual Studio. Now here inside Visual Studio we have our checkpoint script. The first thing we'll need to do is when the car enters the checkpoint we'll want to display a message on the screen. So we'll need to get access to the text object that's part of the user interface the text object that we created in the proceeding movie to display the countdown message. So here inside Visual Studio, I'm going to go on to the top here, where it says using Unity engine and add an additional namespace by choosing using Unity engine dot UI and then I'll create a reference to the text object that we're going to be referencing. So that's going to be public text, and then I'll say UI text like so. Could equal the initial value of null. The next thing I want to do is to remove the start and the update function, because we won't be needing those. The only kind of event that we're really interested in knowing about is when the player has entered that trigger and Unity gives us a really convenient function to do that. It's called on trigger enter. Here's how we create that. I'm going to choose private, void, on trigger enter. And there are some variations on trigger enter is when an object enters the trigger on trigger exit is when an object leaves a trigger and then on trigger stay happens every single frame for as long as an object is inside the trigger. But we're going to be using on trigger enter here. And then notice that this function has a parameter called other which is the object that entered the trigger. So at this stage, I want to check that this object has the player tag that I'm expecting. So in this case, I'm going to choose if other dot compared tag is player I'll put the exclamation mark in front of it to say if it's not the player, then simply terminate the function at this point. Meaning that if we reach this line here, it will be because the player has entered the trigger. Now at that point, that's where I want to display our congratulations message. And in this case, I'm simply going to print UI text dot text equals checkpoint reached, like so. I could of course add a string variable up here if I wanted to customize that data inside the inspector. At this point, I'm going to choose file and save and go back to Unity. So back here inside Unity, the only thing that I really need to do here is to select the checkpoint object. And here we have the box collider attached but we don't have the script. So I'm going to move to the script here, the checkpoint script, and drag and drop that onto the checkpoint. Now you'll see from the inspector that our script is expecting a reference to the text object that it's going to be using. So in this case, I'm going to make sure that I grabbed the text object. That's the countdown object that we created in the previous movie. I'm going to drag and drop a countdown object on to that here, and we're ready to hit play on the tool bar and let's see what happens. So I'm going to press play on the toolbar and we're going to get three, two, one, go and we can move the call forwards, that's great. Now I know that the checkpoint is around about here and in moving past it, we don't get the message displayed. And the reason for that is because if I check out the family car here, you'll see that it has a rigid body and that's fine. It also has the tag that says player, and that's great too. But one ingredient is missing. When you take a look at this object it doesn't have a collider. Some of its child objects do have colliders. We can already see those being visualized inside the viewport, but this object itself, the family car doesn't have a region that is going to interact with the trigger. And we need to add that. It's really easy to do. I'm going to move down here to add component and click on add component. And inside the typing field here I'm simply going to type box collider, press enter on the keyboard to add a box collider component. You can see here that a box collider has been added to the car. I'm going to mark that as is triggered to and begin to size this, to surround the car object itself. So grab the Z field, great. And then the same thing for X here. And I want to try to keep this pretty tightly around the car. So I'm just going to increase just the Y just a little bit, but not too much. Great. Now let's hit play again. Three, two, one, go. And then the message disappears. The car moves into action, we can move the call forwards. And now this time we have hit the checkpoint and it tells us checkpoint reached. We were able to detect that collision because the car had a rigid body, it had the box collider, it had the player tag and we had our checkpoint over here with our script attached, using on trigger enter. Using those combination of ingredients, you can detect when any moveable object enters a specific region of the level and then respond appropriately. This is incredibly important and critical behavior for when you're creating any kind of game, visualization or simulation.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.