Creating A Stamina Bar w/ New Unity Input system
Objective: Learn how to build a stamina bar that depletes when holding the sprint key and recharges when the key is released.
Table of Contents
· Add a UI Slider
· Registering Input
· Input Callbacks
· Coding the Stamina Bar
∘ Sprint Start
∘ Sprint Cancel
· Executing the Sprint Action and the Interactive Stamina Bar
· Bonus Code Tip on Courtines:
· The Result
Add a UI Slider
🎨 Want your slider to look like the one in the thumbnail? Here’s a quick style guide:
Start by adding a UI Slider to your scene.
- Remove the handle
- Change the Background color
- Edit the Fill Area
- Edit the Fill
Once styled, you’re ready to move on!
Registering Input
We’ll jump straight into the coding and won’t cover how to set up Input Actions or scripts in this guide.
💡 Need help setting up Input Actions Scripts? [Click here]
💡 New to the Input System? [Start here].
First, we declare two global variables :
🔍 Code Breakdown:
_slider
: A reference to your UI Slider, made accessible in the Inspector using[SerializeField]
._isSprinting
: A flag to track whether the player is currently sprinting.
Input Callbacks
Using Unity’s event-based callback system, create two methods:
- One for when sprint starts (key is pressed)
- One for when sprint stops (key is released)
Note: When using event notation, Unity auto-generates the callback methods for you. Just press Tab and then Enter after referencing the action.
For now, test the callbacks by printing messages to the console.
Coding the Stamina Bar
🧠What we need: The slider value should decrease while sprinting and increase while resting.
💡Solution: Use a coroutine to update the slider value over time based on the
_isSprinting
state.
Sprint Start
Let’s create our coroutine. We’ll start by creating the condition to track when the Player is spriting.
Code Breakdown
- By using the boolean variable we created earlier, we can now actively track when the player is sprinting.
- While this variable is
true
, the stamina slider's value decreases over time—this is achieved usingTime.deltaTime
, which ensures the change is smooth and frame-rate independent. - A
Debug.Log
statement is included to display the slider value during development for troubleshooting purposes. Be sure to remove it in the final version of your code. - Typically, coroutines use
yield return new WaitForSeconds()
to introduce a delay between executions. However, since our logic needs to update every frame without a fixed delay, we useyield return null
instead. This keeps the coroutine running continuously, once per frame.
Sprint Cancel
Continuing within the same coroutine, we add logic to increase the slider value whenever the boolean is false
.
The coroutine as a whole should look like this:
Executing the Sprint Action and the Interactive Stamina Bar
In your sprint start and cancel methods, update the _isSprinting
flag and start the coroutine. The coroutine is only called from the Start method.
🔍 Code Breakdown:
- The boolean is set to
true
when the sprint key is pressed, andfalse
when it’s released. - We call the coroutine from the start input method to ensure the stamina bar responds appropriately—decreasing while sprinting and recharging when the key is no longer held.
Bonus Code Tip on Courtines:
If you want to control how long it takes for the coroutine to decrease or increase, simply divide the values by the number of seconds.
The Result
When divided by 10, the refill speed is noticeably slower. (Note: The GIF is cut short to keep the file size down — it doesn’t instantly refill. What you’re seeing is the GIF looping.)
Thanks for reading!
If you found this helpful, feel free to check out my portfolio or follow me here — I’m currently open to opportunities in the video game industry! :)