Step-By-Step Guide To 2D Animations In Unity

Unity provides a powerful animation system that allows developers to bring their 2D games to life with dynamic and engaging animations. In this tutorial, we’ll dive into 2D Animations in Unity and as we bring dynamic life to your characters. You might also be able to do unity sprite animations and unity 2D character animation in unity

2D Animations In Unity

Animating Player Character In Unity:

Animating a player character in Unity adds life to your game. Here are the key steps to bringing your character to life.

1. Import Your Animations

Begin by preparing your 2D animations. Import sprite sheets or individual sprites for idle and running animations into your Unity project. Make sure that the sprite sheets are properly sliced to represent the animation’s various frames.

import your animations

2. Attach an Animator Component to Your Player

In the Unity Editor, choose your player character, then in the Inspector window, click “Add Component” and search for “Animator.” Attach the Animator component to your player to provide for animation control.

attach animator component on player

After that you should also create a animator controller and attach it to the controller section of animator component.

create a animation controller

Now drag the animation controller to the animator component.

drag the animation controller to the animator component

3. Create an Animation and Organize

To create a new animation, right-click in your project window and pick “Create,” then “Animation.” Name the animation, for example, “playerIdle,” and keep everything organized in its own folder within your project.

create-animation

And then name it correctly with the naming convention being in camel case.

name created animation

4. Set Up Animator Window

The Animator window will contain all of your animations. Make sure you have it open by going to “Window” > “Animation” > “Animator.”

And Then drag your “playerIdle” to the animator while selecting the player.

It will then automatically connected from Entry as it is the first animation being added.

drag animation to animator

5. Open Animation Window and Set Up Idle Animation

Open the Animation window, then pick your “playerIdle” animation and drag the sliced sprite sheets into it. Set the sample rate (speed) to your desire.

open animation window

Now drag all your animation to the animation window while selecting the right animation from the animator.

Drag Animation sprites Into The Animator

Change the sampling rate to change the speed, because you might want to change the animation speed.

set desired speed

6. Test The Animation

Now, start the game and check if the animation is working, it should work if you did everything correctly.

Note: You can use the character controller from this article by us.

7. Create and Set Up Running Animation

To build a new animation called “playerRun,” use the same methods as above. Drag the cut sprite sheet into the Animation window, then change the sampling rate.

First of all, create a new animation named “playerRunning”.

create running animation

Then drag animation into the animator.

Drag Running Animation Into The Animator

8. Set Up Transitions for Running Animation

Go to the Animator window and build transitions between the idle and running animations. This enables for smooth transitions between the two animations.

Make transition from Idle to Running.

Make animation from Idle.

Make transition from Running to Idle.

Make transition from Running to Idle

9. Introduce a Boolean Parameter

Create a bool parameter in the Animator window and name it “isRunning.” This setting governs the transition between idle and running animations.

Create a boolean parameter.

Name it “isRunning”.

name boolean parameter

10. Configure Transitions with Conditions

Select a transition in the Animator panel, then navigate to the Inspector and enter a condition in the Conditions area. Select the “isRunning” option and set it to true for the “go” arrow and false for the “coming back” arrow.

Configure Transitions with Conditions

Just remember to do these things before going forward.

Remember to uncheck has exit time checkbox

11. Scripting to Control Animations

In your player controller script, define an Animator variable. In the Start function, make reference to the Animator. Use if-else expressions to control the “isRunning” parameter based on the horizontal input.

private Animator animator;

void Start()
{
    animator = GetComponent<Animator>();
}

void Update()
{
	if (horizontalInput > 0)
	{
	    animator.SetBool("isRunning", true);
	    
	}
	else if (horizontalInput < 0)
	{
	    animator.SetBool("isRunning", true);
	    
	}
	else
	{
	    animator.SetBool("isRunning", false);
	}

}

12. Flip Sprite Based on Direction

Create a variable for the SpriteRenderer component and set the “flipX” property based on the horizontal input to control the character’s facing direction.

private SpriteRenderer spriteRenderer;

void Start()
{
        animator = GetComponent<Animator>();
        spriteRenderer = GetComponent<SpriteRenderer>();
}

void Update()
{
	if (horizontalInput > 0)
	{
	    animator.SetBool("isRunning", true);
	    spriteRenderer.flipX = false;
	}
	else if (horizontalInput < 0)
	{
	    animator.SetBool("isRunning", true);
	    spriteRenderer.flipX = true;
	}
	else
	{
	    animator.SetBool("isRunning", false);
	}

}

13. Implementing Jump Animation

To add a jump animation, follow the same steps. Create a new animation called “playerJump,” and use your script to activate the jump animation based on player input or collision detection.

Note: This is now a test, you have to yourself configure and set the jump animations to use this code and test your leaning ability.

void Update()
{
	if (horizontalInput > 0)
	{
	    animator.SetBool("isRunning", true);
	    spriteRenderer.flipX = false;
	}
	else if (horizontalInput < 0)
	{
	    animator.SetBool("isRunning", true);
	    spriteRenderer.flipX = true;
	}
	else
	{
	    animator.SetBool("isRunning", false);
	}

if (isJumping)
{
   animator.SetTrigger("Jump");
}
}

This article explains how to add animations to a 2D platformer in Unity. Feel free to expand on these ideas and adapt the animations further based on your game’s needs and now you should be able to do any kind of 2d animations in unity related to unity 2D character animation.

Full Code With Movement Controller:

Below is attached the full code of the player character with all the movement and animations.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    private Rigidbody2D rb;
    public float speed = 5f;
    public float jumpHeight = 10f;
    private Animator animator;
    private SpriteRenderer spriteRenderer;
    private float horizontalInput;

    void Start()
    {
        animator = GetComponent<Animator>();
        rb = GetComponent<Rigidbody2D>();
        spriteRenderer = GetComponent<SpriteRenderer>();
}

    void Update()
    {
        if (Input.GetButtonDown("Jump"))
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpHeight);
        }

        horizontalInput = Input.GetAxisRaw("Horizontal");

        rb.velocity = new Vector2(speed * horizontalInput, rb.velocity.y);
        AnimatePlayer();

    }

    void AnimatePlayer()
    {
        

        if (horizontalInput > 0)
        {
            animator.SetBool("isRunning", true);
            spriteRenderer.flipX = false;
        }
        else if (horizontalInput < 0)
        {
            animator.SetBool("isRunning", true);
            spriteRenderer.flipX = true;
        }
        else
        {
            animator.SetBool("isRunning", false);
            
        }
    }
}


Learn all about 2D animations in unity from the official unity docs:

#2danimationsinunity #unityspriteanimation #unity2dcharacteranimation #2danimationunitytutorial

You Might Ask:

Can I use any sprite sheets for character animations in Unity?

Yes, Unity supports various sprite sheet formats. Import them for character animation versatility to start doing 2D animations in unity.

Why use the Animator component for character animation?

The Animator provides a powerful framework for controlling and transitioning between animations seamlessly for unity sprite animation.

Is it necessary to organize animations into separate folders?

Organizing animations enhances project management and keeps your assets structured and accessible.

How do I ensure smooth transitions between idle and running animations?

Set up transitions in the Animator window, linking idle and running animations for fluid character movement.

Can I control animations through scripting in Unity?

Absolutely. Scripting allows dynamic control of animations, responding to player input or game events.

What is the purpose of a boolean parameter like ‘isRunning’ in the Animator?

‘isRunning’ serves as a switch, enabling conditional transitions between animations based on player actions.

How can I manage the direction of my character during animations?

Utilize sprite rendering techniques, like flipping the sprite along the X-axis, to reflect movement direction dynamically.