How to create the Animations (Idle & Walk) in Unity 2D

Oceans of Terra
3 min readMay 25, 2023

--

In this lesson about developing 2D games using Unity, we will focus on generating animations and configuring the animator for our player. Initially, our objective is to produce two animations: one for the idle state and another for running. Once we have created these animations, we can proceed to set up the animator for the player and establish transitions between the animations. Lastly, we will modify the parameters of our animator through some code added to our player controller script, enabling automatic animation transitions.

First thing you need to do is open up Window → Animation → Animation

Next is you’ll see a button that allows you to Create a new animation and you just have to click on that and perhaps create a new folder “Animations” inside your Assets folder and save this file as “Idle”

Next is click Add Property and expand the Player Game Object and then expand Sprite Render and then..
you’ll see the Sprite option for that proprty and just click on the plus size to add it.
Then you just have to open your Sprite file and drag that sprite unto the Animation keyframe
Next thing we need to setup our Animator. Go to WIndow → Animation →Animator
Next is we need to click on the Parameters tab, then click on the plus sign and select Float and name your parameter “Speed”
Next is right-click on Idle and select Make Transition
And then point the arrow created to Run to make that transition
Next is click on the white arrow going from Idle to Run and on the Inspector it will bring you a setting that allows you to un-check Has Exit Time, change Transition Duration to 0, and then set the Speed Conditions to Greater than 0.1, then just do it again for the Run -> Idle transition but now with Less than 0.1 speed condition.

Then you just need the add the code script below. Create a new file under Scripts folder called OOT_PlayerController.cs and just copy & paste the code below.

OOT_PlayerController.cs

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

public class AU_PlayerController : MonoBehaviour
{
//Components
Rigidbody myRB;
Transform myAvatar;
Animator myAnim;
//Player movement
[SerializeField] InputAction WASD;
Vector2 movementInput;
[SerializeField] float movementSpeed;


private void OnEnable()
{
WASD.Enable();

}

private void OnDisable()
{
WASD.Disable();

}


// Start is called before the first frame update
void Start()
{
myRB = GetComponent<Rigidbody>();

myAvatar = transform.GetChild(0);

myAnim = GetComponent<Animator>();
}

// Update is called once per frame
void Update()
{
movementInput = WASD.ReadValue<Vector2>();

if (movementInput.x != 0)
{
myAvatar.localScale = new Vector2(Mathf.Sign(movementInput.x), 1);
}

myAnim.SetFloat("Speed", movementInput.magnitude);
}

private void FixedUpdate()
{
myRB.velocity = movementInput * movementSpeed;
}

}

--

--

Oceans of Terra
Oceans of Terra

Written by Oceans of Terra

0 Followers

With OOT players can connect w/ friends, complete missions, defeat enemies, hunt for treasure, mine for gemstones, and much more.

No responses yet