PlayerAnimation.cs 1.01 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
using UnityEngine;
using System.Collections;

public class PlayerAnimation : MonoBehaviour {

    private Animator animator;
    private Vector2 lastInput = new Vector2(1, 1);
    private int horizontalDir = 1;

	// Use this for initialization
	void Start () {
        animator = GetComponent<Animator>();
    }
	
	// Update is called once per frame
	void Update () {
        AminateMovement();
    }

    void AminateMovement() {
        Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
        if (Mathf.Abs(input.x) > 0 || Mathf.Abs(input.y) > 0) {
            lastInput.x = input.x;
            lastInput.y = input.y;
        }
        if(lastInput.x < 0 && input.y == 0) {
            horizontalDir = -1;
        }else {
            horizontalDir = 1;
        }

        animator.SetFloat("X", Mathf.Abs(lastInput.x));
        animator.SetFloat("Y", lastInput.y);
        Vector2 scale = transform.localScale;
        scale.x = horizontalDir;
        transform.localScale = scale;
    }
}