- Home /
Reverse animation after playing with a button
I have a simple animation of up and down. So if i press and hold A, it goes down and stops whenever I release it.
private Animator Anim;
private bool flu = false;
void Start()
{
Anim = gameObject.GetComponent<Animator>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
Anim.SetBool("flu", true);
Anim.speed = 1f;
}
else if (Input.GetKeyUp(KeyCode.A))
{
Anim.speed = 0;
}
But I wanted to make it go back aka reverse the animation when i press and hold S, and stops when I release it. I tried:
private Animator Anim;
private bool flu = false;
void Start()
{
Anim = gameObject.GetComponent<Animator>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
Anim.SetBool("flu", true);
Anim.speed = 1f;
}
else if (Input.GetKeyUp(KeyCode.A))
{
Anim.speed = 0;
}
else
{
if (Input.GetKeyDown(KeyCode.S) && flu)
{
Anim.speed = -1f;
}
else if (Input.GetKeyUp(KeyCode.S))
{
Anim.speed = 0;
}
}
}
But it didnt work, care to help?
yeap, its set to true.
It works fine when i pressed A, it stops when i release the button. Question is how do i reverse the animation (maybe with a speed of -1?) when i press and hold down S key.
Answer by ThatHanGuy · Jan 16 at 08:21 PM
Solved. To anyone that might need this in the future. Setting speed to -1 apparently doenst work in unity script. So you would need to add a float and add it to the multiplier in the animator.
Then the script is as follow:
private Animator Anim;
private bool move= false;
void Start()
{
Anim = gameObject.GetComponent<Animator>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Q))
{
Anim.SetBool("move", true);
Anim.SetFloat("Speed", 1.0f);
}
else if (Input.GetKeyDown(KeyCode.W))
{
Anim.SetBool("move", true);
Anim.SetFloat("Speed", -1.0f);
}
else if (Input.GetKeyUp(KeyCode.Q) || Input.GetKeyUp(KeyCode.W))
{
Anim.SetBool("move", false);
Anim.SetFloat("Speed", 0);
}
}
Your answer

Follow this Question
Related Questions
Imported baked fbx anim makes weird quick move during animation and go back again? 0 Answers
How to stop animations on different layers playing simultaneously? 0 Answers
Can I make animations snap to a frame? 1 Answer
Can the Animation Controller be used to create a sequence from multiple objects & animations 0 Answers