I have a code that is suppose to make the character sprint much like in Half-Life 2. You press [shift] and while you press shift a counter counts to 300 and player speed is increased. When it hits 300, player speed is reset to 10. The counter begins to count down as soon as [shift] is released. When the counter hits 0 again, the player is able to sprint again.

The code sort of works, the problem is, the counter is behaving odd. Sometimes it counts too fast, usually when I'm moving, which means I can't run for the full 300 count. It ranges from 1-5 seconds. I need it to be constant.

Code:
var sprinting;			//a counter that increases as Suzetta sprints
var tired = 0;			//toggles when sprinting hits max

function sprint()
{
	if(key_shift)	//if shift is pressed
	{
		if(tired == 0)	//if tired is zero
		{
			if(sprinting < 300)	//if the sprinting variable is less than three hundred
			{
				sprinting += (sprinting < 300);	//increase by 1 if sprinting is less than max, constrained to max
				player.speed = 60;					//set player speed to 60
			}
			else						//if sprinting variable is greater/equal to than 300
			{
				tired = 1;				//set tired to 1
				player.speed = 10;	//set player speed back to 10
			}
		}	
		else
		{
			sprinting -= (sprinting != 0);	//reduce sprinting by 1 if sprinting isn't 0
			tired = (sprinting != 0);			//set tired to 0 when sprinting hits 0
		}
	}
	else										//if shift is released, allow sprinting to reduce back to 0
	{		
		sprinting -= (sprinting != 0);	//reduce sprinting by 1 if sprinting isn't 0
		tired = (sprinting != 0);			//set tired to 0 when sprinting hits 0
		player.speed = 10;					//set speed back to 10
	}

}


it is called in the player action, at the bottom of the rest of the movement script.


I was once Anonymous_Alcoholic.

Code Breakpoint;