2D Animation
Contents
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:
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.
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
Create animated character frames in Aseprite
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.
Make sure your program still works ok.
Create an idle animation for your character like we did before.
Make sure that works. Now we need a clip for walking/running. Add a new clip from the Animator tab:
Show how to play clip, and change clip.
See how both clips show up in Animator.
Add a parameter, and transitions:
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}