Make sprite animation with Unity


Make an animation sheet

· Prepare a png in which pictures to be animated are arranged in squares of the same size vertically and horizontally.

When using two or more animation patterns, arrange them so that the patterns flow from upper left to lower right

Click "Project > image"

  • Change TextureType to Sprite and SpriteMode to Multiple in Inspector
  • Click Apply at the bottom of the Inspector to reflect changes
  • Click the SpriteEditor button in the middle of the Inspector

  • As SpriteEditor window appears, set Slice> Type to GridByCellSize
  • Set division size to size of one frame of animation (arrange images of 128x128, tc.)
  • Since Pivot will be the center when turning it, it is good to keep it at the foot (Bottom) if it is a humanoid
  • When you are done, press Apply in the upper right corner of the SpriteEditor window
  • Animation patterns created under the image hierarchy are put side by side

Create animation controller & animation

  • Multiple selections with command (Ctrl) + click to pattern animation
  • Drag and drop selected animation patterns to Scene at once
  • Animation file save dialog is displayed so save it with a name
  • GameObject for Hierarchy and AnimatorController for Project automatically
  • Since AnimatorController is the name of animation pattern, change to an appropriate name
  • Since GameObject is also the name of the animation pattern, change (Player or something)
  • Double click on AnimatorController and confirm
  • If an orange state with the same name as Animation is added inside

Adjust animation

  • Animation is played when playing
  • Select animation created in Project
  • If you play once and end it, check Inspector> LoopTime

If the animation speed is fast / slow, change the value of Animation Window> Samples

Add animation

  • Create an animation sheet for the animation you want to add
  • Multiple selection with command (Ctrl) + click to pattern animation
  • Drag and drop selected animation patterns to Scene at once
  • Animation file save dialog is displayed so save it with a name
  • GameObject is created in Hierarchy and AnimatorController is automatically generated in Project, so delete it
  • Change the name of the remaining Animation, drag & drop it to the first AnimatorController
  • Double click on the first AnimatorController to confirm
  • Gray state with the same name as Animation added

Examples of using animation. Animation is not a state but it is moving directly with Play().

// MyPlayerControl.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace chickSample{
    public class MyPlayerControl : MonoBehaviour {
        private readonly float maxSpeed = 3f;
        private readonly float moveForce = 10f;
        Rigidbody2D rb;
        Animator anm;
        SpriteRenderer sprRend;
        bool isJump;
        // Use this for initialization
        void Start () {
            rb = GetComponent<Rigidbody2D> ();
            anm = GetComponent<Animator> ();
            sprRend = GetComponent<SpriteRenderer> ();
        }

        // Update is called once per frame
        void FixedUpdate () {
            if (Input.GetKey (KeyCode.LeftArrow)) {
                rb.AddForce(Vector2.left * moveForce,ForceMode2D.Force);
                anm.Play ("ChickLeft");
                sprRend.flipX = false;
            }
            if (Input.GetKey (KeyCode.RightArrow)) {
                rb.AddForce(Vector2.right * moveForce,ForceMode2D.Force);
                anm.Play ("ChickLeft");
                sprRend.flipX = true;
            }
            if (Input.GetKey (KeyCode.UpArrow)) {
                rb.AddForce(Vector2.up * moveForce,ForceMode2D.Force);
                anm.Play ("ChickUp");
                sprRend.flipX = false;
            }
            if (Input.GetKey (KeyCode.DownArrow)) {
                rb.AddForce(Vector2.down * moveForce,ForceMode2D.Force);
                anm.Play ("ChickDown");
                sprRend.flipX = false;
            }


            if (rb.velocity.magnitude > maxSpeed) {
                rb.velocity = rb.velocity.normalized * maxSpeed;
            }

            AnimatorStateInfo stateInfo = anm.GetCurrentAnimatorStateInfo (0);

            float speedRate = rb.velocity.magnitude / maxSpeed;
            anm.speed = stateInfo.speed * speedRate;
        }


        void OnCollisionEnter2D(Collision2D other){
            if (other.gameObject.name == "Sphere") {

                Vector2 dir = other.gameObject.transform.position - gameObject.transform.position;
                dir = dir.normalized;
                Rigidbody2D otherRb = other.gameObject.GetComponent<Rigidbody2D> ();
                if (otherRb != null) {
                    otherRb.AddForce (dir * moveForce, ForceMode2D.Impulse);
                }
            }
        }


        void OnTriggerEnter2D(Collider2D other){
            if (other.gameObject.name == "Cube") {
                StartCoroutine(itemGetCo(other.gameObject));
            }
        }


        IEnumerator itemGetCo(GameObject otherObj){

            BoxCollider2D coll = otherObj.GetComponent<BoxCollider2D> ();
            coll.enabled = false;
            float timer = 1f;
            while (timer > 0f) {
                timer -= Time.deltaTime;
                otherObj.transform.localScale *= 0.9f;
                otherObj.transform.Rotate(Vector3.one*10f);

                yield return null;
            }
            Destroy (otherObj);
            yield break;
        }
    }
}

AUTHOR

READ NEXT

Boostlog is an online community for developers
who want to share ideas and grow each other.

Delete an article

Deleted articles are gone forever. Are you sure?