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 begin by organizing our triggers into a single game object to clean up our hierarchy. And then duplicating our fire trigger, and taking a look at how we can use tags instead of the name to trigger our script. So here's where we left off. I've just opened up lesson 29. So we have our two triggers here. And I'm just going to turn their mesh renderers back off. And let's also begin organizing our triggers in our hierarchy. Because I don't like them just laying around. So I'm just going to create empty game object. And we can just call this our triggers. And let's just drag all of our triggers into this new game object. So our fire trigger and our two door triggers. Now we also are only being damaged by this initial fire. So let's just cue up our particles. And select our fire trigger one. And let's just Control D, duplicate, this. And move this over to the rest of our fire triggers, or our fire areas. We can also hit R and scale these up to match the actual area of this fire. Now I really don't like having multiple triggers named the same thing. Because then it's difficult to come in and let's say remove one of them with the activate trigger script. Because if you could recall in an earlier lesson, the activity trigger script sees all names, or if multiple objects are all the same name, it becomes incredibly difficult trying to figure out which is which. Now to duplicate this I'm just hitting Control D. And then just moving it over to where it needs to go. So Control D and moving this to the last area. So I'd like to give these different names. So let's come in and give these various names. So fire trigger four, fire trigger three, two, one. And we're 43. Not exactly, fire trigger three. And one. However, this is going to now break our script. Because if we come in and if we enter fire trigger two, we can see here that the if statement will not evaluate. Because it's looking for a name of fire trigger one. Now there's a few ways around this. We could do something called an or statement. Which is simply two vertical lines which are right under your Backspace. We could do if the collider's name is fire trigger one, or the collider's name is fire trigger two. And this should work. Paste this in here. And let's just change this to fire trigger two. I'm just going to move over here. Do fire trigger two. So now this if statement says, if the collector's name is fire trigger one, do this. Or, if the collider's name is fire trigger two, do this. So let's hit Control S to save that. And come back and see where our fire trigger two is. So that's the one in this hallway here. So let's just test that out. We run forward, fire trigger one. Owe, owe, owe. Open the door. And now let's come over here to fire trigger two. And this should work. Owe, owe, owe. But if we come back and try something like fire trigger three or four, we'll see that it does not execute the script we want it to. Of course I need to run over to where that is. So let me run into this room here. And a fire trigger three. Nothing happens. I am now essentially fireproof. So we can take this and continue to write in these or statements. But as you can see, the line is going to get incredibly long. And we're going to have to add in code for every single fire trigger we ever add to our level. And if we have 20 different fire triggers, this is going to take forever. So instead of using this method, let's actually use tags instead of names. So I'm just going to remove this new piece of code here. And let's go back into Unity and see how we can tag all of our fire triggers with the term fire. And that way we can test if these objects, which have very different names, are tagged with the fire tag. So that's found in the Inspector up here in the top. And you'll see there are some default tags that are built automatically into Unity. But we want our own tag. so let's click Add Tag. And I see this panel here mixes people up quite often. You see, we have a bunch of layers here. And so you might be tempted to click over here and type in the tag in this area. But these are actually the layers. To add a new tag we need to dial down this tags. And add something in to elements zero. So let's click over here on the right side. And let's just say, fire. So now we need to go to each of our fire triggers and change it's tag to fire. So let's just come in here, select those, and change the tag to fire. And now we should be able to see-- it didn't do it for all of them. So we'll just need to come in and do it for each of these. Of course if we set up our tags before we started duplicating things, this would not be a problem. OK so now our four triggers are tagged as fire. So now we just need to change our script to accommodate. So let's go back to mono, and let's change-- let's see here. We're testing for the name. So just to double check I'm going to jump back to my handy scripting reference. OK, great. I can check what tag object this is. What the object tag is. And then we can go ahead and remove our health if it is tagged with the fire. So let's go back to mono here. And so now we'll just do the collider dot tag. And let's check if the tag is equal to fire. So now any object tagged with fire, if it's a trigger, will now remove our health. So let's hit Control S, jump back into Unity, and test this out. I'm going to hit play. And run over to our fire. OK that hurts. And just to speed things up, I'm going to hit Control Shift P to pause. And I'm going to change my first person controller to be much faster. That will be under Character Motor Movement. And we're just going to change the forward speed to something like 12. Just so I can run around a little quicker in the game. So let's see here. So let's test our second fire. And that hurts. And let's test our other fire here. That hurts. And we've got a little bit of a collider problem going on. But we can fix that later. And all of our fire hurts. OK, great. So by adding tags in we can now name our objects appropriately but still keep the exact same script functionality that we need to test whether the trigger is a damaging trigger, or if it's something like a door trigger. So now that we've got that issue sorted out, let's take a look at how quickly we're deleting or removing our health and how we can use something called delta time to make sure that our health gets removed at the same speed on any system.