how to stop animation when button is released

Hi I am thirteen years old and am fairly new to unity and have been using java script. I know there is a similar question that someone else has asked but it hasn’t solved my problem.
I am making a fps game and I have made a walking animation for my gun to go up and down as I walk. But the problem is that when I press the ‘w’ key the animation plays but doesn’t stop playing even when you release the ‘w’ key.
here is my script and I would be very grateful if you could help me please. :slight_smile:

function Update()
{
 if(Input.GetKeyDown("w"))
 {
  // Plays the walking-m16 animation - stops all other animations
  GetComponent.().Play("walking-m16", PlayMode.StopAll);
 }
}

if (Input.GetKeyUp(“w”))
GetComponent.().Stop();
… Or perhaps play an idle animation instead of stopping it entirely.

if (Input.GetKeyUp("w"))
    GetComponent.<Animator>().Play("idle-m16", PlayMode.StopAll);

… Or set parameters on the animator instead of controlling which animation to play from code. This requires you to set up animation parameters for the animation controller and set up transitions. If you don’t know how to do that, I recommend you read the manual on Unitys animation system.

var isWalking = Input.GetKey("w");
GetComponent.<Animator>().SetBool("isWalking", isWalking);