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?

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.

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);