Help with an animation script.

function Update()
{
if(Input.GetKeyDown(“w”))
{
animation.Play(“Animwalk”, PlayMode.StopAll);
}
}
I am creating a game. I used some simple code to create this but now, when I test it and push “w” the animation continues to play without end. I am not the best coder and am still green around the ears. If someone finds a solution(s) then please post the code in the comments section.

Let me explain exactly what this code snippet does.

Every frame it checks for whether the ‘w’ key was first pressed that frame. That is to say, Input.GetKeyDown will only evaluate ‘true’ once per key-press, and only at the very beginning.

When the key gets pressed, it tells the animation component to start playing an animation. Keep in mind that this is an instantaneous command, that changes the state of the animation- the animatior component itself will happily continue playing the animation until it is finished or, depending on the looping mode, forever. The ‘StopAll’ command makes sure that there will only be one animation playing at a time.

Currently, your code is doing exactly what you told it to do. What is the actual intended behaviour? You could simple add a matching ‘Input.GetKeyUp’ command, with ‘animation.Stop()’ to force the animation to end at the end of the keypress, if that’s what you were after.