Hi guys!
I am making a sprint system in my FPS game with a duration / stamina bar, and I have almost finished it. There is just one glitch where if the timer goes down to 0, it goes back up by say 0.1, then you use it again. So if you hold onto the shift key, and the duration goes down to 0, you continuously go into run and walking and run and walking. In the code I used
if (Input.GetKey(KeyCode.LeftShift))
, but I think there should be a way to fix it if you play around with
Input.GetKey()
and
Input.GetKeyDown()
though the
Input.GetKeyDown()
is only called on the frame you press down.
Help?
It all depends on the behaviour you want.
For example, a lot of games don’t recharge your sprint while you have the button held down, even if you’ve run out of stamina. So you could just not recharge your player’s stamina while it’s held down.
In any case, can’t provide specific help without seeing more than single lines of code.
Ok so now, i made it so that the duration just depletes if you hold left shift, but when it gets to 0, you are still running. Fix? Here is my code for the whole running code
void Running()
{
if (Input.GetKey(KeyCode.LeftShift))
{
running = true;
}
if (Input.GetKey(KeyCode.LeftShift) && !crouching && RunningTime <= maxRunningTime && RunningTime != 0)
{
speed = 6.5f;
jumpHeight = 1.9f;
anim.SetFloat("Spped", 2.5f);
RunningTime -= Time.deltaTime;
if (Camera.main.fieldOfView != 75)
{
Camera.main.fieldOfView += 2;
}
}
else if (!Input.GetKey(KeyCode.LeftShift) && !crouching)
{
speed = 5f;
jumpHeight = 1.7f;
anim.SetFloat("Spped", 1.5f);
running = false;
if (Camera.main.fieldOfView != 65 && Camera.main.fieldOfView > 65)
{
Camera.main.fieldOfView -= 2;
}
if (Camera.main.fieldOfView != 65 && Camera.main.fieldOfView < 65)
{
Camera.main.fieldOfView += 2;
}
}
}
Edit: dont mind all the animator and camera FOV stuff, just look at all the other code, especially hte if statements and the runningTime stuff, also I would prefer that it does go up if your stamina is out
Well you’re checking if RunningTime equals zero or not. Floating point values will never exactly equal a specific number. You just want to test whether it’s greater than zero or not most likely.