hi!! i’m currently trying to implement a sprinting mechanic with stamina for a game i’m making and every part of it works fine except for one thing: when you keep holding the sprint key until it hits 0, the stamia doesn’t recharge (this also includes if you were to let go of sprint then hold it again)
the intended behaviour is to normally let the player sprint by increasing their speed and camera tilt angle when holding the sprint key (in this case, left-shift). however, once the meter reaches 0, what should happen is that it slows down the player (as if they are fatigued) and not let the player sprint until the bar is full again
literally every other part of this works other than this. is there any way to make the game ignore the input during recharge or is there just a flaw in this code i’m being blind to? any help is appreciated!!!
i think your problem is logic one - because you only remove sprint when you’re not sprinting, try removing else and just leave it as it is, then it should work
Research floating point error. Your stamina will never be exactly 0, so your check stamina != 0 won’t work as you expect it to. Change it to stamina > 0 and it should work, assuming the rest of your code works fine.
This can still be a problem, because computers interpret numbers differently than humans and what seems equal to humans might not seem equal to computers. So that’s still the first thing I would change and see if it works.
also: replacing != with > made the code work the exact same way, and replacing the second clause with not is_zero_approx broke the canSprint check (i.e. it never activates the slower speed and you can immediately sprint again after the bar starts recharging)
If you don’t have stamina and you don’t press the shift
So all of your lines like “if stamina is < 100” or “if stamina == 100” will only work when you’re not pressing shift, and stamina is equal zero, that’s how boolean logic works
if you wanted to check these, i’d suggest you doing it outside if block like that:
@NOTIdealDev unfortunately after testing that solution a couple times over (mostly as sanity checks) using the code you attached just entirely stops the player sprinting
it’s because you need to adjust it, idk how your entire game works, but if you did stamina < 100 and stamina == 100 at the same time, it wouldn’t work
you need to add additional checks, like “if restored stamina” or smth like that
from what i understand you want player to sprint, and if he lost all of his stamina - he need to replenish it back to 100 to sprint again, then adding restoration flag would help, but it requires testing from you
just remember to don’t have these checks inside else statement