Creating a Self-regenerating Stamina Bar with a Cooldown System
I previously wrote an article on how to achieve this using coroutines, this is another approach using a Cooldown System so that the player can’t abuse the sprint or thrust feature. The procedure is similar, and the Slider setup remains unchanged, if you want to check how I got the look for my Slider in this article, please checkout the previous one.
Table of Contents:
· Making a Method to Update the Stamina Bar
· Using Keypresses to call the method
· Regenerating the Stamina Bar
· Adding a Cooldown System
∘ The Result:
Making a Method to Update the Stamina Bar
In this section, we’ll be creating a method to calculate and update changes to the Slider. The following example showcases my code for a 2D Space Shooter thrusting mechanic, however, the same logic can be applied to a stamina bar.
- First, we need the Unity UI library in our code.
2. Now that we can access the UI, we can create a reference for the Slider. This will be a SerializeField so that we can set it in the Inspector.
3. Get the Slider Component within the Start Method.
4. It’s best practice to check if a component is null before attempting to use it in code.
5. Create a global variable for the max value. This value should match the max value you assigned for your Slider in the Inspector. It can be an int or float.
Note: Make sure you checked “Use Whole Numbers” in the Inspector if you intend on using integers.
6. Create a method to calculate and update the Slider/stamina bar. This method will have a numeric parameter to keep track of the current fuel that is being decreased or increased.
7. Within that method, we need to make sure that the total fuel is incremented or decreased by the current fuel variable. This is done by adding the current fuel number to the total fuel using shorthand notation +=
Note: The way the program knows whether the value is decreasing or incremented is through the use of negative or positive numbers. You will see this further a long the code.
8. Since we have access to a variable that keeps track of updating the total fuel by adding the current fuel, we can use this to check if there is enough fuel to keep writing or running. If there is none, you can set the player’s speed to the default value, in my case 4.
Note: The Debug message there is not neccesary but it’s useful to make sure that if is being registered when the Slider is at 0.
Using Keypresses to call the method
- With this setup, you can call the updateThrustGauge method whenever a key is pressed in the Update method, and set a number in the parameter to determine by how much you will be decreasing and increasing the Slider.
In this example, we are using GetKey rather than GetKeyDown because the keypress will be true frame by frame, whereas GetKeyDown is true for that one frame in which you pressed it. What this means is that a calculation that runs in GetKey will happen continuously for as long as you are pressing that key. We need this so that the Slider updates continuously rather than having to press the key repeatedly to make it move.
I also set the player speed to be twice as fast(from the 4 default speed to 8). This is assigned directly rather than doing it through arithmetic because we don’t want the player to becomes faster and faster, and as aforementioned, that’s what would happen with a calculation that is processed in response to GetKey.
2. Now, we need to normalize the speed of the player whenever the key is released. To do this, we use GetKeyUp which registers the same way GetKeyDown does, for that one frame.
Regenerating the Stamina Bar
Now, we need to regenerate the stamina bar/slider whenever it’s not in use. We can’t use the GetKeyUp to do so since it will only register once per keypress. Instead, we will place this in the update and activate it whenever the player is not thrusting. First, we need a bit more to do this.
- Create a global bool variable to keep track of when the player is or isn’t thrusting/spriting. This will be false by default since our player doesn't just start the game spriting or thrusting.
2. Create two methods to set the value to true and false. It’s best practice to have methods handle this rather than changing the value of a variable on the fly.
3. Now we need a method for regeneration. Alternatively, this can be placed loosely in the update, but I feel more organized placing it within a method. This method will become active whenever stopThrusting is called.
Notice how the updateThrustGauge method is being incremented this time.
4.Call the isThrusting method whenever the key is pressed.
5. Call the StopThrusting method whenever the key is released. Since you’re tracking each, the regeneration method will become active and inactive accordingly. Add the regenFule method outside the GetKeyUp, that way it will regenerate the bar continuously whenever thrusting is false.
Adding a Cooldown System
The Cooldown System is quick and simple to implement, you can learn more about it in this article. Let’s take a look at how it would be implemented to this code.
- Create two global variables, one will be the delay, and one will be when you thrust or sprint again. I’m using a SerializeField so that I can see how the value changes in the Inspector, or tweak it myself for debugging purposes. You can tweak these numbers depending on how long you want the player to be restricted from springing or thrusting.
2. Add a condition to start thrusting by comparing the current runtime to our _canThrust variables.
3. Next, we add the delay whenever the key is released. We don’t want this value to increase indefinitely so the delay adds up whenever the key is released.