Animating a sprite in Unity is essentially creating a computerized flipbook. Since a sprite is an image attached to your game object, just swap between different sprites to create an animation.
To create an animation for this Triple Shot Powerup, you would display each of these sprites in rapid succession. Let’s take a look at how Unity allows us to do this.
Creating an Animation
To get started, select the object this animation will apply to in the Scene view. If you need to add the animation to a prefab, drag an instance of the prefab into the Scene and be sure to apply Overrides once the animation is set.
Next, go to the Windows menu and open an Animation window under the Animation sub-menu. Like other windows in the Unity Editor, you can dock the Animation window wherever it fits for your workflow. With your object highlighted, click Create in the Animation window. This will open a File Explorer window to select the save location for the new animation.
Record the Animation Clip
In the Animation window, click the red circle icon to enter Record mode. Then, select all the sprites that make up your animation and drag them into grid in the Animation window (also called a Dopesheet). You can view the animation with the Play button. If no adjustments are needed, click the Record button again to exit Record mode.
Alternatively, you can slow down the animation by stretching out a group of sprites to pause longer between each image.
You can also adjust where individual sprites are listed in the animation. Click in the Dopesheet away from the sprites to de-select the group. Then, click and drag the diamond for a particular sprite to move it.
Animators, Controllers, and Clips (oh my!)
When following the workflow above to create a new animation, Unity creates three things in the background to support the animation:
- Animator Component
- Animator Controller
- Animation Clip
The Animator component links the animation to the object. There are a few settings here, but the most important is the assignment of the Controller.
The Animator Controller is essentially the brain behind the animation for an object. Here you can set the decision tree for which animation should play when. The Triple Shot Powerup is very simple example with only one animation clip that is always playing. However, imagine if you were animating a human as your player. Depending on user input and various scenarios, you might need an animation clip for running, jumping, attacking, etc. In the Animator Controller, you set the conditions for each clip and how to transition between animations.
An animation clip is the actual group of images displayed for the given scenario, which you built in the Animation window. In the Inspector for the Animation Clip, you can make a few adjustments — most notably Loop Time. By default, Unity will loop through sprite animations. If that does not fit your needs, uncheck Loop Time to play through the animation only once each time its activated by the controller.
Since the controller determines which animation clip is active, there are also additional adjustments that can be made to the animation clip from inside the controller. If you click on the Triple Shot Powerup animation clip from the Controller, you can see the window above. You can control the animation speed here as well, as opposed to in the Animation window.
By combining these features in Unity, you can build eye-catching animations in Unity to level-up your 2D projects.