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.
Hi. I'm Chris. And in this lesson, we're going to take a look at how to create a new animation asset, and using Animation Window to animate our doors open and closed. And then, in the next lesson, we'll be able to create a trigger to activate our animation. So here's where we left off. I'll just open up 24, or Lesson 24, from our lesson scenes here. And currently, if we hit play, our doors are blocking our progress. Now, we don't yet have a collider on them, so they technically are not. But at the end of the game, we would want these doors to actually open and activate when we get close to them. However, for them to animate, we need to actually add an animation onto them. So I'm going to hit Control P to get out of my play testing stage. And you can add an animation in your 3D application and use an animation clip in that way. But we can also add animations here inside of Unity. So to do that, let's begin by creating a new animation asset. So let's go to Assets, Create Animation. And let's just call this door open left. And these names are just organization. We could have named this upside down door explosion, and depending on the script, it would do whatever we animate it to do. So now I'm going to drag this up into my animations folder, again, just to keep things clean. And now, with this selected, let's drag this onto our door. Now, I'm not sure which one of these is the door we're looking for. So we can actually drag it on inside of our scene view here. And if I select this door, we can see it has been plugged into the animation port. I'm just going to turn off my lights here so I can see a little easier. And let me just double check-- yeah, I had a suspicion that this was happening. So as you can see here, if we dial down our animations, we have two animations. Now, I would rather just have the door open. So let's just right click Res Vert Count value to prefab, and that will clean up our animations window. That happens when we have animations already ready on an object and we need to come in and edit them or replace them. So now we have our animation on our door. But of course, if we hit play, nothing is going to happen because we have not yet set any key frames. So again, Control P to get out of that. And let's go over to our animation window. So this is going to be under Window Animation, or Control 6. And you can see here, we have all these controls, or all this information on the left. Now, this only happens, or these properties are only visible, if we have an object selected. Now, you'll notice if there's no animation component on that object, these are grayed out. However, if we have an animation component with an animation clip inside of it, these are now able to be set key frames on. So now, let's begin adding some key frames. To do this, we need to begin by hitting this Record button. Now, you'll notice our timeline appears here, so we can change the frame count, or the time, when we set these key frames. Now, initially, I would like this door to begin closed. So we want to set a key on the current time, or the current property. Now this button here sets a key frame, but as you can see, if we click it, nothing is happening. And that's because Unity assumes we do not want to set a key frame on everything, so it asks us that we click on whatever we want to set the key frame on. Now, since this is just a simple door animation, we only need to animate this position x. So let's select that and hit Add Key Frame. And now you can see that key frame appears in our animation window. Now, a key frame is simply a container for a value at a certain time. So it's just saying, on frame 0, or at the start, we want the position x to be 32. Now, let's scroll forward to about 30, and let's move this over to the left. And now we're saying, at this frame, or at this time, I want it to be 35. So now let's hit Play up here. And we can see our animation play through. Now, depending on your animation window when you open this up, the animation timing may have been off. Now you notice I just hit F to frame in. And we have our usual controls, Alt Left click to pan, Alt Right click to zoom in and out, and of course, the scroll wheel to zoom in and out as well. So again, I just hit F here. And you can re-time these by clicking and dragging on a key frame. You could also move it up and down to change the value. But I really prefer holding Shift if I'm trying to just change the timing of this key frame. So you can see holding Shift, we can change whether it's a fast animation or a slower animation. So let's hit Play again. And it's a little bit boring. So to give it a more hydraulic feel, let's give it an ease out key frame. So let's right click, or let's select our last key frame here, and right click and go to flat. And that's just going to flatten up the tangency, so our door begins by moving quickly and then slows down as it enters the final key frame. Now, it's still a little fast, so I might even want to pull it out to maybe even 40. Play around with your animation. Of course, depending on whatever effect your trying to create, this is going to be different on your own project. So now that that's done, we have our door animated. So let's just walk through that process one more time for the door open right. So again, we'll go into a create a new asset animation. And we'll give it some descriptive name, in this case door open right. And we need to apply it to our object, which we can do by just dropping in on. And then we need to select that object, go to the animation panel, turn on recording, select the attribute or property we want to animate, and begin setting down key frames. So I'm going to click Add Key Frame, and again jump forward to, I think it was around 40, and then move this door over to the right. And just like last time, I'm going to hit F, select the last key frame, and right click and flatten that out. So now if we hit Play, we can see we have our right door animated correctly. Great. So now let's make sure on both of these doors play automatically is turned on. And let's hit play. And now we can see both of our doors open as soon as the player begins moving around, or as soon as the game starts. Now of course we actually want this to trigger at a different time. So we actually do want the Play Automatically turned off if we're going to trigger this animation using a different object or a collision. So let's just clean up our project a little bit. And we're good to go. Now, there's one more thing I want to take a look at. And this can be a little bit confusing sometimes, so I'm going to try to go a little slowly. Animations inside of Unity are always absolute on the object level. So if we want to move our doors to a different location, let's say if we want to move them backwards, this is going to work until we turn the animation on. As soon as we turn on play animation, or as soon as we trigger the animation, these doors are going to jump forward to their first key frame and then move and animate to their last. So if we hit play, we'll see our door jump forward and then finish in the original position. To see this, let's hit Control Shift P. And let's look at where our door is. So we can see we moved it backwards. However, the sliding door right has jumped forward because that is where the animation key was set. So again, we cannot move an object with animation on it and expect it to change its behavior. It will jump to wherever it was animated from. Now, you might be asking how do we get around this. And the answer is simply game-object parenting. So let's unplay this. And I'm just going to Control Z a few times to get my doors back into the correct location. And now let's see how we can use parenting to move these objects while keeping the animation on them. So to begin with, we need to create a new game object. So go to Game Object, Create Empty. And we can call us whatever we want. I'm just going to call this door left transform. And again, we can call this anything. Now, you'll notice that our game object started right here. And this is actually a problem. If we parent the current game object, or the current sliding door animation to this game object, it's going to fly off in space. That's because it's going to add this position to the door's current position and essentially do a double transformation. So on this new game object, let's reset the transform position back to 0. And I'm actually going to call this S Door so it appears in my hierarchy near my other ones. And let's now parent the door to this new game object. And I'm just going to give this a unique name so I can find it in my hierarchy. So here we have our door that is animated. And we need to parent it by just left clicking and dragging it under our new game object. Now, if you have center tool handle turned on, you can now move this game object, and it will work correctly. If you have pivot turned on, you'll see our pivot point is back at the origin of the scene. And this could be a little difficult, so let's turn this back to center. And now we have a transformation object storing the movement information as an offset so that we can move this door anywhere. And now, if we hit Play, the door will still animate correctly if, of course, animation is turned on. So if we turn on Plays Automatically and hit Play, we can now see our door animates no matter where we put it. So again, this is how we can create a relative transformation for an animated object. And it's very simple. Just parent the object into a new game object that has been zeroed out. And then you can use this new transform, or this new game object, to move your object around. Now, I just snapped this door back by holding the V Key and using our vertex snapping to return it to its original position. I could've also gone in here and zeroed out the transforms. And that should have worked, but looks like it didn't. So I'm going to use vertex snapping, and now my door will play correctly. Well, we still have it there. And that's because, again, this transform is not 0. So let's zero those out. And then, I believe I accidentally moved this door, so let's snap that back in. And that's one of the things we always have to be careful about when we apply an animation. We no longer want to move this base object. We always want to move it based on this transform. And that can sometimes get us into trouble if we're not careful. But with a little bit of fixing, our door is now animated correctly, and we now can duplicate this door and move it into the other positions for the rest of the scene. So I'm just going to keep doing this step. I'm going to do it exactly the same with the sliding door right to replace this pair of doors and this pair of doors here with these animated doors. And once we have those set up in the next lesson, we'll come in and we'll call create triggers to open these when the player is close.