Learning Unity — Animating Sprites
In the last article, we looked at how to make more complex game objects by joining together multiple simple objects. In this article, we’re learning how to utilize Unity’s Animation feature in order to make animated sprites. Animation of 2D sprites in Unity is a relatively simple process, but takes games to a whole new level of polish.
The first thing we’ll need is to import the collection of sprites that will make up our animation. Right-click in the Project window and select “Import New Asset…” to import image files into the project. You’ll probably want to organize them into their own folder. After they are imported into the project, make certain that the Texture Type of the files is set to “Sprite (2D and UI)”. You will not be able to use them in an Animation if they are not of the correct type.
The next thing we’ll need is a Sprite object in our scene. If you don’t know how to create a Sprite object, take a look at my article on transitioning from 3D primitive objects to 2D sprites. Chances are you’ll want to create the sprite from the first frame of your animation, though not necessarily.
Once we have our Sprite object in place, open the Animation window and make sure your Sprite object is selected in the Hierarchy window. You should see an option to create an Animation Clip in the Animation window.
Click the “Create” button, and you will then be taken to a save file dialog. Give your animation file an appropriate name and location and then save it. Unity will actually create two files at this time — an Animation Clip and an Animator Controller.
An Animation Clip represents a distinct animation for an object, such as jumping or running. An individual object could have many different Animation Clips to represent various actions the object is capable of performing. An object that is animated in some way will definitely have at least one Animation Clip file, and could potentially have several.
An Animator Controller is what manages these Animation Clips and the in-game logic that determines what animation (if any) should be playing from one moment to the next. An individual object will have only one Animator Controller associated with it.
With the Animation Clip created, now we need to actually build our animation. This is actually pretty easy, if you have your artwork organized sequentially. In my example project, my animation is of an explosion, and the individual frames are numbered and in order.
The files being organized, all that needs to be done to create the animation is to select all of the files that will be a part of the animation and drag them over to the Animation window. Unity does the rest.
And that’s all that is necessary to create the animation! You should be able to see, if you run the game, that the animation will start and loop endlessly by default.
If we wanted to change the looping aspect of the animation, that can be easily done by finding the animation clip file in the Project window and then adjusting the settings in the Inspector. For example, if we didn’t want a loop at all, then we would just uncheck the “Loop Time” option.
And that concludes our introduction to animations in Unity. I think you can see just from this simple example how potent animations can be to create a more engaging gaming experience. In the next article, we are going to learn about making our in-game effects temporary. Until then, good luck and happy coding!