How I can do stop animation when the key let go?
I am doing a controller script for my character and I finally do my character play walk animation when key is pressed, now i dont know how to do animation stop when the key let go. The character Rig it's not humanoid is Legacy, her is the part of script: public void animationsplayercontrol() {
animation.Play("anim25");
if (Input.GetKeyUp(KeyCode.W) || Input.GetKeyUp(KeyCode.D) || Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.S))
{
animation.Play("anim10");
}
}
Answer by KoenigX3 · Jun 04, 2020 at 07:39 AM
If it's the same animation you want to start and stop using keys, try this:
void Update()
{
if(Input.GetKeyDown(keycode)) animation.Play();
else if(Input.GetKeyUp(keycode)) animation.Stop();
}
Replace keycode with the actual keycodes, and animation with the reference to your animation.
I think that I need to do that stop the animation when WASD it's not pressing or something like this (I'm learning C# and really don't know) because its a controller script and when I press D after press W the animation stop because W was let go while pressed D
this is the code now //Idle animation.Play("anim25");
//Run
if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.S))
{
animation.Play("anim10");
}
else if (Input.GetKeyUp(KeyCode.W) || Input.GetKeyUp(KeyCode.D) || Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.S))
{
animation.Stop();
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613000038im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Help with OnTriggerEnter Not working correctly? 1 Answer
Jump over obstacles(runner game) by speech recognition 1 Answer
Setup an animation script, but now mouselook is not working? 0 Answers
How I can do stop animation when the key let go? 0 Answers
CharacterController.Move called on inactive controller 0 Answers