2D animation brings your sprites to life by switching between multiple images quickly. Instead of drawing every movement manually, you create a sequence of frames that play in a loop. Before animating, you need sprite images. You can get them from:
- Asset Store: Window - Asset Store - Search "2D character sprite".
- Sprite sheet: Download a PNG file with multiple frames.
Importing and Slicing a Sprite Sheet
A sprite sheet contains all animation frames in one image. You need to slice it into individual sprites.
- Drag the sprite sheet image into the Project window
- Click the image - In Inspector - Texture Type - "Sprite (2D and UI)" and Sprite Mode - "Multiple".
- Click the "Sprite Editor" button
- In the Sprite Editor window, click "Slice" - Type - "Automatic" - Click "Slice".
- Click "Apply" (top right).
Output: Your single image splits into multiple sprites. You will see "Idle_01, Idle_02, Walk_01, Walk_02" etc. in the Project window.
Creating Idle Animation
Idle animation plays when the character is standing still.
- Select all idle sprites (Idle_01, Idle_02, etc.).
- Drag them into the Scene view.
- Name the animation "Idle" when prompted.
- Press Play – character animates in place.
Creating Walk Animation
- Open Animation window (Window - Animation).
- Click dropdown - "Create New Clip" - Name it "Walk".
- Select all walk sprites - Drag into timeline.
Switching Between Animations
- Open Animator window (Window - Animator).
- Parameters tab - "+" - Float - Name "Speed".
- Right-click Idle - Make Transition - Click Walk.
- Click transition arrow - Inspector - Uncheck "Has Exit Time" and Condition: Speed > 0.1.
- Walk - Idle transition with condition: Speed < 0.1
Script to Control Animation
using UnityEngine;
public class PlayerAnim : MonoBehaviour
{
private Animator anim;
void Start()
{
anim = GetComponent<Animator>();
}
void Update()
{
float speed = Mathf.Abs(Input.GetAxis("Horizontal"));
anim.SetFloat("Speed", speed);
}
}
Attach script to character. Press Play and use arrow keys.