Animating Sprites in Unity: A Beginner’s Guide

Damian Dąbrowski
3 min readFeb 6, 2024

--

Unity is a powerful engine that’s not only renowned for its ability to create complex 3D environments but also as a robust platform for 2D game development. One of the fundamental aspects of creating engaging 2D games is sprite animation. Whether it’s a character running, an enemy attacking, or a simple UI effect, animation brings your game to life. In this article, we’ll cover the basics of animating sprites in Unity, ensuring you have a solid foundation to start adding dynamism to your 2D projects.

Understanding Sprites and Sprite Sheets

Before diving into animation, it’s crucial to understand what sprites are. In 2D games, a sprite is essentially a graphical asset that represents characters, objects, or other elements. Often, animations are created using sprite sheets — a single image file containing all the animation frames for a sprite.

Setting Up Your Sprite Sheet in Unity

  1. Import Your Sprite Sheet: Drag and drop your sprite sheet into the Unity Editor’s Assets folder. Unity will import it as a texture.
  2. Configure the Sprite Sheet: Select the imported sprite sheet in the Assets folder. In the Inspector window, change the Texture Type to `Sprite (2D and UI)` and the Sprite Mode to `Multiple` since our texture contains multiple sprites. Click `Apply`.
  3. Slice the Sprite Sheet: With the sprite sheet still selected, open the Sprite Editor from the Inspector. Click on `Slice`, choose `Automatic` in the Slice menu, and then `Slice`. Unity will automatically detect and slice individual frames. Make adjustments if necessary, then click `Apply`.

Creating an Animation

With your sprites ready, it’s time to animate:

  1. Create an Animation Clip: In the Project window, right-click in your Assets folder, go to `Create > Animation`, and name your new animation clip.
  2. Open the Animation Window: Go to `Window > Animation > Animation` to open the Animation window. Dock it somewhere convenient.
  3. Assign Your GameObject: Drag the GameObject you want to animate from the Scene or Hierarchy into the Animation window. This tells Unity which GameObject the animation clip is for.
  4. Add Sprites to the Timeline: With the Animation window open and your GameObject selected, you can now drag and drop the individual sprites from your sliced sprite sheet onto the timeline. Arrange them in the order they should animate.
  5. Adjust Frame Rate: By default, Unity sets the animation to 60 frames per second (fps). This might be too fast for your needs. Adjust the Samples value in the Animation window to change the fps, effectively speeding up or slowing down the animation.
  6. Preview and Adjust: Use the Play button in the Animation window to preview your animation. Adjust the sprites’ order and timing as needed until you’re satisfied with the result.

Implementing the Animation in Your Game

With the animation clip created, you can control it through Unity’s Animator component:

  1. Add an Animator Component: Select your GameObject in the Hierarchy, then add an Animator component via the Inspector (`Add Component > Animator`).
  2. Create an Animator Controller: In the Assets folder, right-click and select `Create > Animator Controller`. Name it, and then double-click to open it.
  3. Assign Animation Clips: Drag your animation clip into the Animator Controller window. This creates a state based on your animation clip.
  4. Control Animations with Scripts: You can now control this animation through scripts by accessing the Animator component. For example, to play your animation, you might use:
Animator animator;
void Start()
{
animator = GetComponent<Animator>();
animator.Play("YourAnimationName");
}

Animating sprites in Unity might seem daunting at first, but it’s a straightforward process once you understand the basics. By following these steps, you can start bringing your 2D characters and objects to life with engaging animations. Remember, great animations can significantly enhance the player’s experience, making your game more immersive and enjoyable. So, experiment with different animations, and don’t be afraid to get creative!

--

--

Damian Dąbrowski
Damian Dąbrowski

Written by Damian Dąbrowski

Hi, I’m Damian, an Electrical Power Engineer, who loves building AI powered apps.

No responses yet