'Problem when pressing W and then A with W not working anymore UNITY

I tried to make a code for my animations and I wrote this:

if (Input.GetKeyDown("w"))
        {
            animatorz.Play("SlowRun");
            Debug.Log("ya");
        }

Technically it all worked BUT when I press W and then, for example, A while pressing W I let go from A and it send me back to idle state but I was still pressing W, is there a way to make it properly like in actual games? Thanks!



Solution 1:[1]

The GetKeyDown method only fires once. You want GetKey instead, which fires every frame: https://docs.unity3d.com/ScriptReference/Input.GetKey.html

Using this you'll want to check in the loop if the animation is already playing so you don't restart it every frame the player presses the key, use GetCurrentAnimatorStateInfo e.g. https://answers.unity.com/questions/362629/how-can-i-check-if-an-animation-is-being-played-or.html

Solution 2:[2]

Welcome to Stack Overflow. This script will make it to where you can play the specified animation on the correct key press.

if (Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.A))
        {
            animatorz.Play("W_Animation_Name");
            Debug.Log("W Animation");
        } //This will allow the animation to keep playing. 
if(Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.A)){

     animatorz.Play("A_Animation_name);
     Debug.Log("A Animation");
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Absinthe
Solution 2 Mintvbz