Home > other >  Problem when pressing W and then A with W not working anymore UNITY
Problem when pressing W and then A with W not working anymore UNITY

Time:03-05

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!

CodePudding user response:

Welcome to Stack Overflow! This script will allow the animation to continue playing!

if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A))
        {
            animatorz.Play("SlowRun");
            Debug.Log("ya");
        } //This will allow the animation to keep playing. 

You can add more keys by simply adding

|| Input.GetKey(KeyCode.YourKey) 

to the if statement like I have done above.

CodePudding user response:

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

  • Related