Mario Game in Unity 6 (2025) | Part 3: Adding Animations (Idle, Run, Jump)

 

Mario Game in Unity 6 (2025) | Part 3: Adding Animations (Idle, Run, Jump)



Animations are what make a game truly come alive. In Part 3 of this series, we’ll add animations to Mario for idle, running, and jumping states using Unity 6. This step will transform our simple character movement into a polished and interactive experience.

By the end of this tutorial, you’ll learn how to set up animations, configure the Animator Controller, and write C# scripts to handle animation transitions based on player input.


What Are We Doing in Part 3?

  • Importing already-sliced Mario sprites for animation.
  • Creating animations for idle, running, and jumping.
  • Setting up an Animator Controller.
  • Writing scripts to control animation transitions dynamically.

Step 1: Importing the Sprite Sheet

1.1. Import the Sprite Sheet

Download File
  1. Download the pre-sliced Mario sprite sheet provided in this tutorial.
  2. Drag the sprite sheet into your Unity project’s Assets folder.

Since the sprite sheet is already sliced, there’s no need to use the Sprite Editor. The sprites will appear as individual assets in the Project panel.


Step 2: Creating Animations

2.1. Create Animation Clips

  1. In the Project panel, create a new folder named Animations.
  2. Drag the appropriate sprites for idle, run, and jump into the Scene view to create separate animation clips for each state.
    • Name the clips Idle, Run, and Jump.

2.2. Configure the Animator Controller

  1. Right-click in the Animations folder and create a new Animator Controller. Name it PlayerAnimator.
  2. Assign the PlayerAnimator to the Mario character by selecting the GameObject in the Hierarchy and attaching the Animator Controller in the Inspector.
  3. Open the Animator window and drag the Idle, Run, and Jump animations into it.

Step 3: Adding Animation Transitions

3.1. Set Up Animator Parameters

  1. In the Animator window, create two new parameters:
    • Running (Bool)
    • Jumping (Bool)

3.2. Configure Transitions

  1. Create transitions between the Idle, Run, and Jump states based on the parameters:
    • Idle → Run: Add a condition where Running is true.
    • Run → Idle: Add a condition where Running is false.
    • Idle/Run → Jump: Add a condition where Jumping is true.
    • Jump → Idle/Run: Add a condition where Jumping is false.

Step 4: Writing the C# Script

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public float speed = 7f;
    public float jumpForce = 12f;
    public LayerMask groundLayer;
    public Transform groundCheck;
    public float groundRadius = 0.7f;

    private Rigidbody2D rb;
    private bool isGrounded;
    private float horizontalInput;
    private bool facingRight = true;

    private Animator animator;

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {  
        horizontalInput = Input.GetAxis("Horizontal");
        isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, groundLayer);
        if(Input.GetKeyDown(KeyCode.Space)&& isGrounded)
        {
            rb.linearVelocity = Vector2.up * jumpForce;
            animator.SetBool("jumping",true);
        }
        else if (isGrounded)
        {
            animator.SetBool("jumping",false);
        }
        if(horizontalInput != 0)
        {
            animator.SetBool("running",true);
            Flip(horizontalInput);
        }
        else
        {
            animator.SetBool("running",false);
        }
       
    }

    // FixedUpdate is called once per frame
    void FixedUpdate()
    {
        rb.linearVelocity = new Vector2(horizontalInput * speed, rb.linearVelocity.y);
    }

    void Flip(float horizontal)
    {
        if(horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
        {
            facingRight = !facingRight;
            Vector3 theScale = transform.localScale;
            theScale.x *= -1;
            transform.localScale = theScale;
        }
    }
}


Step 5: Testing Your Animations

  1. Play the game and move Mario using your movement controls from Part 2.
  2. Observe Mario transition smoothly between idle, run, and jump animations as you interact with the game.

What’s Next?

Now that Mario can move and animate beautifully, it’s time to make the game more interactive. In Part 4, we’ll add collectible coins and enemies to enhance gameplay and challenge the player. Stay tuned for the next part!


Watch the Video

Check out the step-by-step video tutorial for Part 3 on YouTube for a detailed walkthrough:


Join the Series

Follow this blog and subscribe to the YouTube channel to stay updated with the next parts of the Mario game development series. Let’s keep building this classic game together!


#Unity6Tutorial #MarioGameDevelopment #2DGameAnimations #UnityAnimator #LearnUnity #GameDevelopment

Comments