• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

how to run with "stamina"?

R

Riyu

Guest
does anyone know how to make my character run like he has a stamina bar, and when that bar runs out he goes back to normal?
 

Slyddar

Member
Just think logically about what you want to do, in order to achieve your goal, and write each step down. It's then easier to code it.

- create a stamina variable, which is the current stamina, as well as a stamina_max variable, showing the maximum amount it can be.
- create the normal walk speed, walk_spd, as well as the speed while using stamina, run_spd.
- while the player is pressing a direction, check if stamina is greater then zero, and if so set a temp variable, say move_spd to run_spd else set move_spd to walk_spd. Also decrease stamina per step if run is pressed. Use move_spd to move the character using whatever method your game uses.
- if the player is not pressing run, increase stamina but don't let it go above stamina_max.
 

NightFrost

Member
You'll also need to solve the issue of run-stutter when run button is continuously held down. When you're out of stamina the player drops back to walk speed, but after regenerating even the tiniest bit, they run for a frame or two, then drop back to walk speed, etc, creating a silly little stutter. For this, you implement an out_of_stamina variable that starts at false. When stamina drops to zero due to running, you set it to true. When you do a run check, you also require that the variable is false, otherwise you don't allow running. When set to true, you let it go back to false on either of two conditions: the player is no longer pressing run button, or stamina has regenerated for N steps or perhaps to A amount, where N and A are set to taste.
 

Fredrik

Member
CREATE EVENT:
Code:
stamina    = 100;
walkspeed =   0;
STEP EVENT:
Code:
if stamina > 0
     {walkspeed = 50;} else
           {walkspeed = 25;}

if keyboard_check_pressed(ord("W"))
     {
     walkspeed = 50;
     stamina -= 0.01;
     }
This is a shabby example. but you'll get the idea..
 

Slyddar

Member
You'll also need to solve the issue of run-stutter when run button is continuously held down. When you're out of stamina the player drops back to walk speed, but after regenerating even the tiniest bit, they run for a frame or two, then drop back to walk speed, etc, creating a silly little stutter.
If the first line of holding run removes stamina, then unless the stamina regen is greater then the amount consumed per step, this will never happen, as stamina will always be zero when the run button is held.
 

NightFrost

Member
If the first line of holding run removes stamina, then unless the stamina regen is greater then the amount consumed per step, this will never happen, as stamina will always be zero when the run button is held.
True, but I think fast regeneration and slow run consumption is a good idea.
...or I may have just played too much Dark Souls 3 recently.
 

samspade

Member
does anyone know how to make my character run like he has a stamina bar, and when that bar runs out he goes back to normal?
You might want to look at something like this - it's used for a jetpack, but the idea is the exact same:

 
Top