2D Animation

In this tutorial we’ll work on animating sprites.

Create a time-based animation in Aseprite

Create a folder for your animation.

Follow one of these tutorials:

https://64.media.tumblr.com/3c0287fd61ebbe01450b857a5536688a/tumblr_pj7xw9oPbZ1x0hs8ho1_540.gifv

Source: SadFace-RL Fire Animations

https://64.media.tumblr.com/f38af30218383e32db327943e6a039e2/tumblr_pjru2fQEYf1x0hs8ho1_540.gifv

Source: SadFace-RL Water Animations

Work on using:

  • Keyboard shortcuts

  • Select tool

  • Frames

Export a sprite sheet.

  • File->Export Sprite Sheet

  • Output->Output file

Import a sprite sheet in Unity

Import a sprite sheet and slice it like we did before.

../../_images/import_sprite_sheet.png
  • Drag the first image onto your scene.

  • Click Window…Animation

  • Click your object, you should see an option to create an animation and controller from it.

  • Drag images onto the timeline

  • Too fast.

  • Drag out the frames, slow it down

https://images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com/f/676c614f-d09c-4485-a246-d1ea708273bd/dchb7sq-c8fedde8-961e-47d4-a4a3-495014265fc1.gif?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1cm46YXBwOjdlMGQxODg5ODIyNjQzNzNhNWYwZDQxNWVhMGQyNmUwIiwiaXNzIjoidXJuOmFwcDo3ZTBkMTg4OTgyMjY0MzczYTVmMGQ0MTVlYTBkMjZlMCIsIm9iaiI6W1t7InBhdGgiOiJcL2ZcLzY3NmM2MTRmLWQwOWMtNDQ4NS1hMjQ2LWQxZWE3MDgyNzNiZFwvZGNoYjdzcS1jOGZlZGRlOC05NjFlLTQ3ZDQtYTRhMy00OTUwMTQyNjVmYzEuZ2lmIn1dXSwiYXVkIjpbInVybjpzZXJ2aWNlOmZpbGUuZG93bmxvYWQiXX0.jprvx0joe-4mDnN-XqmEEps-bOklrD1zm01bJIqLadA

Source: SadFace-RL Animation, getting started

Create animated character frames in Aseprite

https://images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com/f/676c614f-d09c-4485-a246-d1ea708273bd/dcisa01-b285151f-0b33-4df3-b42a-641daddf0f2b.gif?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1cm46YXBwOjdlMGQxODg5ODIyNjQzNzNhNWYwZDQxNWVhMGQyNmUwIiwiaXNzIjoidXJuOmFwcDo3ZTBkMTg4OTgyMjY0MzczYTVmMGQ0MTVlYTBkMjZlMCIsIm9iaiI6W1t7InBhdGgiOiJcL2ZcLzY3NmM2MTRmLWQwOWMtNDQ4NS1hMjQ2LWQxZWE3MDgyNzNiZFwvZGNpc2EwMS1iMjg1MTUxZi0wYjMzLTRkZjMtYjQyYS02NDFkYWRkZjBmMmIuZ2lmIn1dXSwiYXVkIjpbInVybjpzZXJ2aWNlOmZpbGUuZG93bmxvYWQiXX0.5A4ky15XzfRqitDMjOxaX0MGbBoYd-UWAg9kcCOLlb4

Source: SadFace-RL Characters, the human male

https://images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com/f/676c614f-d09c-4485-a246-d1ea708273bd/dceoc95-b39fd3b3-61c9-48ca-8bc0-5f1218641935.gif?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1cm46YXBwOjdlMGQxODg5ODIyNjQzNzNhNWYwZDQxNWVhMGQyNmUwIiwiaXNzIjoidXJuOmFwcDo3ZTBkMTg4OTgyMjY0MzczYTVmMGQ0MTVlYTBkMjZlMCIsIm9iaiI6W1t7InBhdGgiOiJcL2ZcLzY3NmM2MTRmLWQwOWMtNDQ4NS1hMjQ2LWQxZWE3MDgyNzNiZFwvZGNlb2M5NS1iMzlmZDNiMy02MWM5LTQ4Y2EtOGJjMC01ZjEyMTg2NDE5MzUuZ2lmIn1dXSwiYXVkIjpbInVybjpzZXJ2aWNlOmZpbGUuZG93bmxvYWQiXX0.HlW8ziFKqTJ0j78jhxsw-5UAPJP7QXqbIkXmmNfXDCk

Source: SadFace-RL Animation, the walk cycle

Get character working with idle animation in Unity

Here’s a video that covers what we are doing:

First, go ahead an import your character animations, then slice up the images.

If you want to replace a character you already have with the animated sprites, (recommended) you can replace the character’s texture by dragging the sprite image to the proper location.

../../_images/replace_sprite.png

Make sure your program still works ok.

Create an idle animation for your character like we did before.

../../_images/create_animation.png

Make sure that works. Now we need a clip for walking/running. Add a new clip from the Animator tab:

../../_images/new_clip.png

Show how to play clip, and change clip.

See how both clips show up in Animator.

Add a parameter, and transitions:

../../_images/transitions.png

Update code:

 1using System.Collections;
 2using System.Collections.Generic;
 3using UnityEngine;
 4using UnityEngine.SceneManagement;
 5
 6
 7public class MyAnimatedCharacterController : MonoBehaviour
 8{
 9    public int score = 0;
10
11    Rigidbody2D body;
12
13
14    float horizontal;
15    float vertical;
16    float moveLimiter = 0.7f;
17
18    public float runSpeed = 5.0f;
19
20    public AudioSource sound;
21    public AudioSource scoreIncreaseSound;
22    public AudioSource scoreDecreaseSound;
23
24    private SpriteRenderer spriteRenderer;
25    private Animator animator;
26
27    void Start()
28    {
29        // Get the rigid body component for the player character.
30        // (required to have one)
31        body = GetComponent<Rigidbody2D>();
32        spriteRenderer = GetComponent<SpriteRenderer>();
33        animator = GetComponent<Animator>();
34    }
35
36    void Update()
37    {
38        // Get our axis values
39        horizontal = Input.GetAxisRaw("Horizontal"); 
40        vertical = Input.GetAxisRaw("Vertical"); 
41    }
42
43    void FixedUpdate()
44    {
45
46        // If player is running diagonally, we don't want them to move extra-fast.
47        if (horizontal != 0 && vertical != 0) // Check for diagonal movement
48        {
49            // limit movement speed diagonally, so you move at 70% speed
50            horizontal *= moveLimiter;
51            vertical *= moveLimiter;
52        }
53
54        if (horizontal > 0.1)
55            spriteRenderer.flipX = false;
56        else
57            spriteRenderer.flipX = true;
58
59        // Set player velocity
60        body.velocity = new Vector2(horizontal * runSpeed, vertical * runSpeed);
61        animator.SetFloat("HorizontalSpeed", Mathf.Abs(horizontal));
62    }
63
64    void OnTriggerEnter2D(Collider2D colliderEvent)
65    {
66        // Did we run into an object that will affect our score?
67        ScoreScript scoreObject = colliderEvent.gameObject.GetComponent(typeof(ScoreScript))
68                                  as ScoreScript;
69
70        if (scoreObject != null)
71        {
72            // Yes, change the score
73            score += scoreObject.points;
74
75           
76            // Destroy the object
77            Destroy(colliderEvent.gameObject);
78        }
79
80        // Did we run into an object that will cause a scene change?
81        SceneChangeScript sceneChangeObject = colliderEvent.gameObject.GetComponent(typeof(SceneChangeScript))
82                                              as SceneChangeScript;
83        if (sceneChangeObject != null) {
84            // Yes, get our current scene index
85            int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
86            // Load up the scene accourding to the sceneChange value
87            UnityEngine.SceneManagement.SceneManager.LoadScene(currentSceneIndex + sceneChangeObject.sceneChange);
88        }
89    }
90    void OnGUI()
91    {
92        // Dispaly our score
93        GUIStyle guiStyle = new GUIStyle(GUI.skin.label);
94        guiStyle.fontSize = 32; //modify the font height
95        GUI.Label(new Rect(10, 10, 250, 50), "Score: " + score, guiStyle);
96    }
97}