I have 2 attack animations named "attacking_1" and "attacking_2", I want to play one at random when player presses "C" key once. I want the animation to continue to play to last frame of it without player needing to hold down the C key.
Here’s my code and what’s happening is that this code is ran every frame and making my player display both attack animations on one C key press.
What I don’t understand is why the if statement is executing many times even tho the player only press the C key once.
if (Input.GetKey(KeyCode.C))
{
int random = Random.Range(1, 3);
Debug.Log(random);
anim.SetBool("attacking_" + random.ToString(), true);
}
>Solution :
"Input.GetKey()"
Checks if a button is pressed down every frame and runs it every frame. What you need is
"Input.GetKeyDown()"
Which only runs it the first frame it’s pressed down
Hope this helps!