Determining how long a key has been held.

Hello I am trying to teach myself C# and in a part of my code I am trying to determine how long a key has been held so I can play a different animation, so for example, you have been walking for four seconds so now you’re are sprinting.

Here is my code for this part,

{if(Input.GetKeyDown(“w”) >=4)
{print(“Run”);}}

I am getting this error “Operator >=' cannot be applied to operands of type bool’ and `int’” and I’m not sure what to do, help please? :slight_smile:

I am just using print now to show that part is working, I’ll add the animation later.

You’ll need to create a variable that holds the time the button was pressed, and then check that against the current time.
The following block will need to be more or less copied for each direction you can sprint, and the downTimeRight (And left, etc) variable should be created in the class.

if (Input.GetKeyDown (KeyCode.W)) {
	float downTimeRight = Time.time;
}
if (Input.GetKey (KeyCode.W)){
	if (Time.time - downTimeRight > 4) {print("run Right");}
	else {print("Right");}
}