Hello,
I have a simple movement and animation script that works great except once a key is pressed, the animation never stops. I’m guessing it involves a if statement or a while loop, but I can’t seem to implement it right. Any help would be amazing.
using UnityEngine;
using System.Collections;
public class rickyMovement : MonoBehaviour
{
private Animator animator;
public float speed = 4;
public bool happen = false;
void Start ()
{
animator = this.GetComponent<Animator> ();
}
// Update is called once per frame
void Update ()
{
animation ();
motion ();
}
void motion(bool happen)
{
if (Input.GetKey (KeyCode.RightArrow))
{
transform.Translate(Vector2.right * speed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.LeftArrow))
{
transform.Translate(-Vector2.right * speed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.UpArrow))
{
transform.Translate(Vector2.up * speed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.DownArrow))
{
transform.Translate(-Vector2.up * speed * Time.deltaTime);
}
}
void animation()
{
var vertical = Input.GetAxis("Vertical");
var horizontal = Input.GetAxis("Horizontal");
if (vertical > 0 )
{
animator.SetInteger("Direction", 2);
}
else if (vertical < 0)
{
animator.SetInteger("Direction", 0);
}
else if (horizontal > 0)
{
animator.SetInteger("Direction", 3);
}
else if (horizontal < 0)
{
animator.SetInteger("Direction", 1);
}
}
}