sprint code behaving irregularly.

Posted By: heinekenbottle

sprint code behaving irregularly. - 11/09/08 17:01

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.
Posted By: testDummy

Re: sprint code behaving irregularly. - 11/09/08 18:51

Code:
// factor in time (time_step), was using frame
// expect faults
// just a guess
var sprinting = 0;  // seconds sprinting
var tired = 0;  // at sprinting == 300, then sprinting > 0
function sprint() {
	if (key_shift && !tired) {
		// if moving?
		sprinting += time_step * 0.0625;
		sprinting = minv(sprinting, 300);
		tired = (sprinting >= 300);
		player.speed = 60;
	} else {
		player.speed = 10;
		if (tired && !key_shift) { // stop pressing key_shift
			sprinting -= time_step * 0.0625;
			sprinting = maxv(sprinting, 0);
			tired = (sprinting >= 0);
		}
	}
}

Posted By: heinekenbottle

Re: sprint code behaving irregularly. - 11/10/08 16:19

Although your script didn't work, the "time_step * 0.0625" part helped immensely and fixed the problem. I reduced the max to 15 (300 ticks was fine but not 300 seconds grin ) and now my character runs for a constant maximum of 15 seconds (give or take a thousandth of a second) and must rest afterward for a constant 15 seconds.

If she doesn't run the full 15 seconds, she still must rest for the time she did run if shift is released, still debating whether I want this effect.

So thanks

Code:

function sprint()		//allows the player to sprint for a maximum of 15 seconds, after which she must rest for the time she sprinted
{
	if(key_shift)					//if shift is pressed
	{
		if(tired == 0)				//if tired is zero
		{
			if(sprinting < 15)	//if the sprinting variable is less than max
			{
				sprinting += time_step * 0.0625;	//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 max
			{
				tired = 1;				//set tired to 1
				player.speed = 10;	//set player speed back to 10
			}
		}	
		else
		{		
			sprinting -= (time_step * 0.0625) * (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 -= (time_step * 0.0625) * (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
	}
}

© 2024 lite-C Forums