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 use colliders, triggers, and the ActivateTrigger script to open our doors when our player gets near. So I've just gone in and I've created the various doorways that we have inside of our sequence, so this last one, the second doorway, and of course our initial doorway. Now, since Plays Automatically is turned off, we need to wait for the player to actually open this door. And since we want it based on proximity, we can use just a simple collider. So to begin with, let's create a new sphere game object. Now, of course, depending on what size or what collision you need, that would depend on what kind of mesh you would use for the collider. But I like the sphere. It's nice and simple. So let's move this into place. And we need to make sure it's large enough that our player hits it when we're close to it. All right, that looks pretty large. And let's just turn off our mesh render just so we don't see it inside of our view port. We don't really need the mesh render component, but it's useful for previewing the area that this is going to take up. Now currently, we have a collider on it, so our player is going to hit this. So for this sphere to talk to the door, we need a script. Now fortunately, Unity comes with some built-in scripts that allow us to do many very common tasks, such as activating things, deactivating them, animating things, and so on. To do this, let's grab our sphere or whatever we want to use as a collider, and let's go to Component, and Scripts, and let's add an ActivateTrigger script. Now, this is going to ask us for a few things. It's going to ask us for a target and what kind of action we want to take on the target when this is triggered. Now, notice this has a script in its parentheses, and this is just a normal script that we could have written ourselves. If you look at our first person controller, all of these scripts have the exact same markings. However, it's a nice, built-in script that does something for us. Now, just for clarity, let's call this our door 1, and let's call this a trigger, just so we know what we're dealing with here. And so we want to target one of our doors. So we want to target the door. So let's type in "door." And let's see here. We have a lot of objects in here. And we want to target the sliding door 1, but I'm not sure-- OK, sliding door L1. So door L1. OK, there it is. Well, we have multiple copies of this. So let's actually come in, and let's just name our objects a little nicer. So I'm going to dial this down, and I'm just going to update these names with the actual numbers that are associated with that parent. Now with that done, let's go back to our door trigger. Let's come back here. And now, we can just type in L underscore 1, and we know we need to trigger this sliding door L1. Now, we have a variety of different actions, and you might think we need to activate it. But actually, we want to play the animation on this object. So let's change the action to animate, and let's see what happens. So let's hit Play and see if we can open this door by hitting this collider. And we run into the collider, but it's not letting us through. That's because it's not set as a trigger yet. So I'm going to hit Control Shift P to pause, and I'm just going to turn on this sphere collider trigger to see if that fixes it. Now, I'll un-pause, and if we move forward, we can see the door opens successfully. So let's hit Control P to turn off our playback. And of course, we're going to need to come back to our sphere collider and turn on IsTrigger. And now, this is successfully opening sliding door left 1. However, it is not yet affecting our right door. So to have it affect this, you might be thinking we can just drag another target onto this script. However, this script was built for a single target. So another solution is let's just add on another ActivateTrigger script. So let's go to Component, Scripts, ActivateTrigger, and let's go to our target here. And let's target our-- I'm sure these are named inappropriately as well. They're not even numbered yet. So I'm just going to take a quick second to come in and give them numbers so that we can quickly and easily find these. And once we have that, we just need to jump back to our door trigger script and connect in the right underscore 1. And we have our sliding door. And again, we'll change this over to animate. And if we hit Play, we should be able to see our doors open as soon as we get close to them. There we go. So now, we've used a built-in script to open up our doors. And we can, of course, come in and do this exact same workflow for these other doors. And let's just take a quick look at how we can do that very fast. So we have our door 1 trigger. Let's just Control D duplicate that. And let's call this our door 2 trigger. Let's hit the W key, move this back to where we want the trigger to happen. And let's just change the two targets. So instead of L1, let's change this to L2, and I'm going to need to come back and select that door 2 trigger and re-select that object. So let's type in L underscore 2, and again, we'll type in R underscore 2 in our second target. And we should be able to see that this now works correctly. So we'll need to walk all the way over there, and for time's sake, I usually move my character controller over to where we're working. But in this case, we can walk it. OK, great. So now we have two of our doors ready to work and completely scripted with only a few clicks and without writing a single piece of code. So definitely dig into this ActivateTrigger script. It's got a lot of very powerful functionality that we can connect up and use very quickly and easily. Now remember, the only thing this ActivateTrigger needs is a collider of some sort of set to trigger, and then it needs to do an action on something. Animate turns on an animation. Activate activates it, so it enables it. And Deactivate actually removes the game object from the world. Now, the rest of these are a little bit different, so, of course, each of these has their own uses. And so now, let's begin taking a look at actually creating our own scripts because, while this ActivateTrigger script is very useful for things like the game logic and for most of our game, we're actually going to need to come in and create our own scripts and our own code to manage our game. So beginning in the next lesson, we'll create a new script asset and begin working with scripting in Unity.