- Home /
WASD release --- change animation
Hi guys, I am making a survival horror game and you can find a shotgun. However when you walk with the shotgun an animation plays that moves the shotgun from side to side. The Idea is that if your release all of the keys "WASD" it returns to the "Shotgun Steady" animation.
I made the following script: (2 animations: steady and walk)
function Update () {
if(Input.GetKeyDown("w")) {
animation.Play("riflewalk");
}
if(Input.GetKeyUp("w")) {
animation.Play("riflesteady");
}
if(Input.GetKeyDown("a")) {
animation.Play("riflewalk");
}
if(Input.GetKeyUp("a")) {
animation.Play("riflesteady");
}
if(Input.GetKeyDown("d")) {
animation.Play("riflewalk");
}
if(Input.GetKeyUp("d")) {
animation.Play("riflesteady");
}
if(Input.GetKeyDown("s")) {
animation.Play("riflewalk");
}
if(Input.GetKeyUp("s")) {
animation.Play("riflesteady");
}
}
When I Press W to move forward.. the animation plays, but I also press A to strafe a little bit to the left.. No problem the animation plays nicely. But when I release A again to move in a straight line, the animation stops (because of the 4 release commands)
Is there a way to combine all of those release commands in 1 return to the "steady shotgun" animation?
Answer by Piflik · Oct 27, 2012 at 05:42 PM
Something like that might work:
if(Input.GetKey("a") || Input.GetKey("s") || Input.GetKey("d") || Input.GetKey("w"))
animation.CrossFade("riflewalk");
else
animation.CrossFade(riflesteady);
Thanks man! I really like how it fades to the new animation! And it only changes to the Rifles$$anonymous$$dy animation when wasd are not pressed. Exactly what I wanted, Thanks!
After I changed a couple of things it worked out fine :)
function Update () { if(Input.Get$$anonymous$$ey("a") || Input.Get$$anonymous$$ey("s") || Input.Get$$anonymous$$ey("d") || Input.Get$$anonymous$$ey("w")) animation.CrossFade("riflewalk");
else animation.CrossFade("rifles$$anonymous$$dy");
}
Answer by tmar2 · Oct 27, 2012 at 05:52 PM
In each of your tests for GetKeyUp, you need another statement that tests whether or not another key is currently down. So in your example, once "A" is released, before you change to the steady animation, first make sure that "W, S, or D" are not currently down. This should solve your problem.
Thanks for the reaction! But the answer above is exactly what I want.
Your answer
![](https://koobas.hobune.stream/wayback/20220613082947im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
idle > walk > run animation 1 Answer
Changing specific animation of a person 1 Answer
Animation... Unity 3D...Blender...3rd Person 2 Answers
Blender Animation Looping Delay Problem 2 Answers