- Home /
Stamina for running doesn't recharge like it should
I have a script for walking and running. When the player runs the speed changes and the "stamina" decreases and when the player isn't running the speed is back to normal and the "stamina" increases. When the Stamina is 0 and the player is holding down Shift, the Stamina shouldn't increase, the player should just not be allowed to move faster. But when the Stamina is less than 0 and the player IS holding down the run button, the stamina increases, and decreases very rapidly. I have a headbobbing script that is affected by the running, so when the Stamina is 0 the headbobbing speed gets faster and back to normal very rapidly, just like the stamina does. How do I fix it? Here's that relevant part of the script:
if (Stamina > 0 && Input.GetKey(KeyCode.LeftShift) && cc.velocity.magnitude > 0f)
{
walkingSpeed = 16.0f;
sideSpeed = 10.0f;
Stamina -= Time.deltaTime;
IsRunning = true;
}
else
{
walkingSpeed = 8.0f;
sideSpeed = 5.0f;
Stamina += Time.deltaTime * 0.75f;
IsRunning = false;
}
Answer by HenriMR · Nov 24, 2019 at 02:14 AM
I think that what is happening is when your stamina < 0 your if-statement returns false and the else part is run. If you want the stamina not to recharge while shift is down, replace "else" with :
else if ( ! Input.GetKey(KeyCode.LeftShift))
Hope it works :)
It's still not working, I don't get why, what you gave should've worked but apparently it didn't for some reason... When I run and the sta$$anonymous$$a is finished the bobbing is still faster than when normal (Thanks for the help <3) I've fixed it, and now it's like this:
if (Sta$$anonymous$$a > 0 && Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftShift) && cc.velocity.magnitude > 0f)
{
walkingSpeed = 16.0f;
sideSpeed = 10.0f;
Sta$$anonymous$$a -= Time.deltaTime;
Headbobber.bobbingSpeed = 0.3f;
Headbobber.bobbingAmount = 0.25f;
IsRunning = true;
}
else if (!Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftShift))
{
walkingSpeed = 8.0f;
sideSpeed = 5.0f;
Sta$$anonymous$$a += Time.deltaTime * 0.75f;
Headbobber.bobbingSpeed = 0.1f;
Headbobber.bobbingAmount = 0.15f;
IsRunning = false;
}
Oh, I think that i should writen like this:
else
{
walkingSpeed = 8.0f;
sideSpeed = 5.0f;
if (!Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftShift)) { Sta$$anonymous$$a += Time.deltaTime * 0.75f; }
Headbobber.bobbingSpeed = 0.1f;
Headbobber.bobbingAmount = 0.15f;
IsRunning = false;
}
Just out of curiosity how did you solved it?
I'm sorry but what's over the else? And how did I solve what? Thanks for the help btw I'm sorry ;( I don't know what to replace with what, shouldn't there be something over the else? And something to make the speed faster?
Your answer
Follow this Question
Related Questions
How do I make a river in the terrain in unity? 3 Answers
Footsteps Script for Running and Walking 3 Answers
Problems with KeyCodes 2 Answers