Join Jesse Freeman for an in-depth discussion in this video Build animations, part of Unity 5: 2D Building an Adventure Game.
- [Instructor] So, now that we've set up all the sprites in our game, and made sure that they're optimized, let's go ahead and start working on some new animations. Before we begin, inside of our game staging scene, let's go ahead and delete all the sprites that we were using before for testing. You can simply click on the first one, and while holding down Shift, select the last one, then right-click and hit Delete. Now, we'll go into our Sprites folder, and then into Player. Here, you'll see all of the animations for our player.
Let's go ahead and select the three frames that we'll use for the player when they're using their jet pack. Click on the first one, and while holding down Shift, click on the third one and drag it into our scene. As you can see, while we drag this into the screen, you'll see the word "Multiple" above the mouse. This let's us know that we're dragging more than on sprite at the same time. When we release the mouse, we'll be asked to create a new animation.
Now, let's go to the root of our Jetroid project folder, and create a new folder called Animations. From here, we're going to rename this animation to Player Jet Animation, and hit Save. If you run the game, you'll see that our player is now animated. Let's take a look at what's going on behind the scenes. Let's go into the Window, and select the Animation window, and then drag this next to our game tab.
And the other window we're going to want to pick is the Animator window, and drag this next to where we put the Animation window. We'll start with the Animator. Here, you can see we have the Player Jet Animation. This window represents the state machine for managing animation transitions. You'll see all the animations attached to a game object that's selected in the hierarchy view. If we select the Player Jet Animation from the Animator window, you'll see in the Inspector, we have a reference to the animation itself, its speed, and some other properties.
Likewise, if we look inside of the animation's folder, you'll see we have the new animation we created called Player Jet Animation, and we now have an animation controller. The animation controller is what is shown inside of the Animator. Let's go ahead and rename this to Player Animation Controller. If we select on the game object inside of our scene, you'll see that we have two components attached to it: our sprite renderer, which is responsible for showing each of the sprites that will be displayed during our animation, and the Animator itself.
Here, you can see, we now have the Player Animation Controller set in the Animation's Controller's property. If we go ahead and select on the Animation tab, and unfold the animation, you'll see each of the frames of the animation. If you run the game, and take a look at the animation, you'll see that it won't run while we're actually playing the game. But if we stop this, and while the game is not running, click Play, we can preview the animations.
You'll also notice that any changes we make change the UI and highlight them to red. This let's us know that something has changed inside of the Animator. One thing that I've noticed in this particular version of Unity is that when you preview an animation, when the game is not running, from the Animation window, it sometimes disables the Animator. Let's go ahead and just re-enable this and save our scene. And now we have our first animation for our player.
- Optimizing sprites and draw cells
- Cutting up sprite sheets
- Building animations
- Changing animations
- Creating a player
- Moving the player
- Building a level with a simple map
- Creating interactive objects
- Creating alien enemies
- Switching between scenes