I want to make my 2D top-down walking animation stop and revert to the idle animation while continuing to face in the same direction as the previous movement.
For example, when I go up then stop, my character should continue to look up. If I go down, it should continue to look down.
I use lastmoveX and lastmoveY floats for idle and for walking I use moveX and moveY floats. moveX and moveY change when I move with the joystick, but lastmoveX and lastmoveY do not change, and I don't know how to fix this.
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private Rigidbody2D myRB;
private Animator myAnim;
public Joystick joystick;
public MoveByTouch controller;
[SerializeField]
private float speed;
// Use this for initialization
void Start ()
{
myRB = GetComponent<Rigidbody2D>();
myAnim = GetComponent<Animator>();
}
// Update is called once per frame
void Update ()
{
myRB.velocity = new Vector2(joystick.Horizontal, joystick.Vertical) * speed * Time.deltaTime;
myAnim.SetFloat("moveX", myRB.velocity.x);
myAnim.SetFloat("moveY", myRB.velocity.y);
if(joystick.Horizontal == 1 || joystick.Horizontal == -1 || joystick.Vertical == 1 || joystick.Vertical == -1)
{
myAnim.SetFloat("lastMoveX", joystick.Horizontal);
myAnim.SetFloat("lastMoveY", joystick.Vertical);
}
}
}