Animating Sprites In Unity
The core mechanics to create 2D and 3D animations in Unity are different just like in the real life. In 2D, the idea to create animations is similar to the cartoons in the past. 2D animations are just multiple frames switching quickly, creating a sense that the objects in the frames are actually moving. Whereas in 3D animations, we need to create actual movements. This article will focus on 2D animations in Unity.
To create animations, we can either use multiple sprites or sprite sheets. If we are going to use a sprite sheet, it is crucial to grid the sprites perfectly in order to have a smooth animation. By placing the sprites into animation, we can make it loop beginning from the first frame to the last just like a flipbook.
To create an animation we first need the animation window open. By clicking Window — Animation — Animation, we can reach to the Animation window. Drag and drop it next to Game view window.
First, select the game object that you want to create animation. Then on the animation window, simply click “Create” and select the folder that we want to place the animation file. If there is no Animation folder in the Assets directory, it will be better to create an animations folder and place the file by adding “_anim” at the end of it’s name will be practical.
If we are going to use a sprite sheet, we need to split into separate sprites first. If you already have multiple sprites, you can continue to “Creating Keyframe Animation” part.
To split a sprite sheet to seperate sprites: Click on the sprite sheet, change the Sprite mode to “Multiple” and then click on Sprite Editor.
In the sprite editor window, you will encounter multiple sprites like below, without the squares.
It is crucial to have properly created sprites in our game. If we have sprites with different resolutions, it would be really hard to slice them in a proper way. Unity offers 3 slicing methods as “Automatic, Grid by Cell Size and Grid by Cell Count”. After clicking slice, we will have multiple sprites placed just under the sprite sheet object in the project window.
Creating Keyframe Animation
Having the multiple sprites ready, now we can continue to the animation creating process. To create the animation, we click on the recording button with red dot, select the sprites that we want to create the animation with and drag them in and lastly click again to stop recording. If you hit play afterwards, you can observe the animation playing in the scene.
That’s it. We have successfully created an animation.
To control the states of the animation, we need to use the Animator window. Just like the Animation window, we can view and then drag it by using Window — Animation — Animator. As our games get more complicated, we will have multiple states and animations to assign. I will be publishing a new article regarding this topic later on.