- Home /
How to get animation to Stop and Start with a key press
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);
}
}
}
Answer by Menzle · Apr 30, 2020 at 02:04 PM
@Effinjerk You can go to the animator, then create a new empty state by right-clicking and selecting "create state" > "empty", and then just do an else statement and play the new empty state. I hope that helped :D
Your answer
![](https://koobas.hobune.stream/wayback/20220613143008im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to move player after animation 2 Answers
Character faces wrong way for one frame only. Why? 1 Answer
Player not moving 1 Answer
How can I add Animation to this player movement script? or do I have to create a totally new one? 0 Answers
why will my jump animation only play on multi-jumps? 0 Answers