What you will learn
In this Unity tutorial, we will learn how to effectively utilize the game engine. Over the tutorial, we will learn how to use the Unity editor interface as well as how to navigate in the scene view. From there, we will learn how to import assets and how to create prefabs. Then we will shape our level even more by adding character controllers, colliders, lights, materials and physics. We will then finish the tutorial by learning how to use scripting in order to make our level interactive.
In this lesson we're going to learn how to set up triggers and then tie them into our script so that our player only loses health when he's near or inside of a fire. So here's where we left off. And I've opened up lesson 27. So currently our character state, if I'll just pull up mono here, has our health being decremented or removed as the update function runs. Now of course we want to actually tie this into the fire. Where the particle system's inside of or scene. So let's begin by just running our particles for a second or two. So I'm just going to select our particles game object here and let them run. So we can see them inside of our scene. Once that's done let's actually come in and create a new trigger. So just as we have in previous lessons, I'm just going to create a new sphere in that rough area. And hit W, and of course use the ALT navigation to move this into around where our fire is. And then hit R to scale this up so that our sphere is completely engulfing or covering our fire. All right so let's just get this right into place. And just like we did last time, we'll make it a trigger and we'll turn off it's mesh renderer. So now we need to figure out how to access this information inside of our script. Now if we go back to mono, there's nothing really that seems obvious about triggers. So whenever I run into this point while I'm scripting, I'm looking for a function, or a command, like GUI box or print, that let's me know when my character or my script has interacted with triggers. So whenever I run into this I always bring up the scripting reference. Now again, this is under Support Documentation Scripting Reference. And let's see-- let's just search this time. So we want to interact with a trigger. So let's just type in trigger. And, as you can see, we get a lot of results. So let's just scan this for seeing what we need. And Unity's great about giving us a plain English sentence about what this does. So is the collider a trigger? Now we don't really need to know that. Oh so this is when our collider enters the trigger. So that might be useful. And this is when it exits. But since the fire is sort of an all encompassing thing, or it needs to continue with time, I'm really more interested when my collider is inside of the trigger. OK great. This next one on triggers stay is called for almost all the frames for every collider that is touching the trigger. OK great. So let's click on this and see what code we need. All right, so it's a function on trigger. And then we need to pass something into this function, which is the other collider. And passing values into functions just gives them information about the scene. So let's just copy this over and take a look at what happens. So let's copy that. Let's go back into mono here. And this is going to be a new function. So we need to put it anywhere outside of the other functions we have. Now you might be thinking, do these functions have to be in a certain order? And we really do not need them in a specific order. That's because these functions are called by our Unity Game System. So it doesn't call update, and then it calls on GUI, and then it calls on trigger stay. What the script is doing is it's waiting inside of our game engine for the game engine to actually say, hey script. Do you have anything that deals with function on trigger stay? And when the script replies, yes, yes I do. I have some code to run. Only then is this function passed back into the game and executed. Now, because I'm a stickler for cleanliness, I'm just going to select these, lines hit Tab. And hit Tab here. And that's just kind of a programming standard. There are of course many different ways to do this. But whenever we have an open bracket, or an open curly bracket as we see here, just going to hold Control and scroll up my wheel so this text can become a little bit clearer. So whenever we have an open curly bracket we're essentially moving in. So some people use Tabs to show that, hey this function here is inside of one, two curly brackets. OK, so now we have this in here let's take Control S to save this script. Jump back to Unity. And let's hit play and see what this results in. Now we may not be able to see this. But let's move in a little bit. No, we can't exactly see this. What they should show up as, as little white lines that are shooting out of the collider. Now that's not exactly what we need. So let's come in and let's modify this now to remove some health whenever we're inside of a trigger. So let's go back to mono here. And you can see-- if it's looking, it has a rigid body attached-- then, and only then, it begins doing things. So we really don't need any of this code. So let's just select it hit Delete. Now we can write the code that we want to execute whenever we're inside of the trigger. So let's just cut our health function, or our health line here. Control X. And let's just paste this into our on trigger stay. So now our health should only be decremented, or only be lessened, whenever we're inside of a trigger. So let's hit Save. And let's jump back to Unity. Now we could update the script while the game is playing. And we can take a look at that in a second. So now we see that if we move forward into the fire, our health gets lower. Now let's test if we move into this trigger here that our health gets lowered. So we're at negative 88. So we move into the door. And up. This is happening for all of our triggers. Now of course we don't want our health to go down whenever we open a door. That's a little bit silly. So we're getting to figure out some code to check if we're actually inside of a fire trigger, or if we're touching a door trigger. But so far we've gotten our triggers to now interact with our script and update our health. And again, that was a very simple process. All we did was we set up a trigger, looked at the scripting resources for every command or every function that has to deal with triggers, found one that works. Plugged it into our mono project. And then simply removed health whenever we're inside any trigger. OK, so now that's done, in the next lesson let's take a look at how we can use conditional statements, or an if statement, to test whether the collider has to do with our fire. Or whether it has to do with a door opening.