I’m working on the basis of my next 2D project and as I’m new to Unity I’m having some problems.
One of them is with animation and movement. I leave a video for you to see:
The change of animation with the movement is not completely fluid, for example, when moving upwards and then to the left the character continues to look briefly upwards, instead of immediately executing the animation of walking to the left.
This is the script I am using:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerController : MonoBehaviour
{
private Animator animator;
public float speed;
// Use this for initialization
void Start()
{
animator = this.GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
var vertical = Input.GetAxis("Vertical");
var horizontal = Input.GetAxis("Horizontal");
if (Input.GetKey (KeyCode.D))
{
transform.Translate(Vector2.right * speed);
}
if (Input.GetKey (KeyCode.A))
{
transform.Translate(-Vector2.right * speed);
}
if (Input.GetKey (KeyCode.W))
{
transform.Translate(Vector2.up * speed);
}
if (Input.GetKey (KeyCode.S))
{
transform.Translate(-Vector2.up * speed);
}
if (vertical > 0)
{
animator.SetInteger("Direction", 0);
}
else if (vertical < 0)
{
animator.SetInteger("Direction", 2);
}
else if (horizontal > 0)
{
animator.SetInteger("Direction", 1);
}
else if (horizontal < 0)
{
animator.SetInteger("Direction", 3);
}
}
}
Thanks.