top of page
Search

Creating the Stamina Mechanic

  • Abbie Hipwell
  • Jan 12, 2022
  • 3 min read

Updated: Jun 14, 2022


We want players of our game to have a limit to how much they can sprint; this not only requires them to be tactical about how often they do this, but also makes gameplay feel a lot quicker - for example running to pick up a ball or powerup.


Stamina is the first mechanic I intend to work on, as this directly effects the sprint, which I will be able to move onto next. The stamina mechanic will work by decreasing in increments of one while the player is simultaneously sprinting; once this reaches zero, the player will no longer be able to sprint. The stamina will then increase in increments of one back up to ten as long as the player is walking at their regular speed.


The screenshot below shows the complete stamina mechanic which I will explain step by step;





Stamina uses Event Tick, which will allow the code to check for an increase or decrease in players speed, alongside their stamina count, every frame.




The variable Stamina is set as an integer, this will store how much stamina the player has. The variable isSprinting? is a Boolean which checks whether the player is sprinting or not.



Stamina Increase


The stamina increase works by first checking whether the player is sprinting or not. If the character is not sprinting, and the stamina is also below ten - the stamina will be increased by one every 0.2 seconds (as shown in the delay node). This will continue until either the stamina has reached the maximum amount (ten), or the player has began sprinting again.


The code below shows the stamina increase:



Stamina Decrease


The stamina decrease works almost identical to the stamina increase. The branch first checks whether the player is sprinting or not; if the character is sprinting and the stamina is above zero - the stamina will be decreased by one every 0.2 seconds. This continues until the stamina has depleted to zero, or the player has stopped sprinting.


The code below shows the stamina decrease:



If the stamina reaches zero at any point, the Boolean variable isSprinting? will be set to False, which will stop the player from being able to sprint for any longer. The players speed will then be set to its regular value of 600.


Currently I have only been able to test that this code works by outputting the value of Stamina with a print node, this is to check that it increases and decreases correctly, without having to code


So far I have only been able to check that stamina is working by printing the value of stamina and checking that it goes up and down correctly. During the testing stage of this mechanic I had to alter the value of the delay node frequently, this was mostly to get the timing of the increase and decrease correct, I also created a placeholder for the sprint by setting the 'shift' key as the binding for this - even though the character currently can't sprint, I am able to check that the stamina is working.


Overall my first mechanic on the game project has gone well and has been made to be as optimised as possible, I think that I achieved this the best way - without any unnecessary code. I'm hopeful that this will work alongside the sprint mechanic which I will create next, if not I will have to come back to this and make changes until they both work together.

 
 
 

Comments


bottom of page