Look Alive! — How to Animate Sprites in Unity
Objective: Learn how to apply simple sprite animation starting from a cropped sprite sheet
Now that we have our power up spawning and behaving as we wanted, wouldn’t it be nice to have it flicking and animating around?
We’re going to learn how to use Unity animation editor and animate sprites in a very simple way if a cropped sprite sheet is available.
A sprite sheet is just an image made of several smaller images (for example representing the frames of an animation). If you have all the parts separated into different images, using the animator will be really easy.
First let’s open the Animator window and attach it as a tab wherever we think is good, for example near the game view.
Then we create an Animations folder in our Asset folder and double click on the power up prefab. Now, in the Animation view, a Create button is visible: click it and save a new animation in the new folder. The name I chose was triple_shot_powerup_anim, appending a _anim suffix will make it easier to find it. Now select all the frames of your animation and drag them onto the Animation view.
You can see that in the Animations folder, there are now 2 files: one is the animation itself, the other is the animator which will allow animation logic handling, if needed.
If you click on the anim file and look at the inspector you will see some simple settings on default value. For example, it is set to loop… and we like it.
If you click on the animator, you will see another kind of view. It is very important to understand it. The diagram shows an entry state in green from which the object will automatically transit, according to the arrow, to our animation to stay there indefinitely, since it is a loop animation.
If you click on the states you can access to some settings and behaviour management. If you right click on a state you can elaborate a transition to other states according to some logic, but that’s material for another tutorial.
Let’s close all these views and look at the game now.
Mmm it seems I forgot to set the layer ordering for the power up!!
Let’s just put it on the same layer and order as the player.