One of the basic features found in many games is a stamina bar that will drain when you’re running or jumping and slowly regenerate when you’re not. So today I’m going to learn how to implement this in Unreal.
This post builds upon work done in my other post Player Stats & HUD with Blueprints.
The end result
Stamina drain when jumping and running.
Setting up the variables
I opened the BPC_PlayerStats
blueprint and created the following variables:
- MaxStamina – float
- CurrentStamina – float
Opening BP_ThirdPersonCharacter
, I set up the following variables:
- WalkSpeed – float. Default value: 500
- SprintSpeed – float. Default value: 900
- isSprinting – boolean
- isJumping – boolean
Creating the sprint functions
Now I need to create the basic functions for sprinting. Going to BP_ThirdPersonCharacter
, I opened the event graph. On the left, I added two functions: StartSprint
and StopSprint
.
For StartSprint
I set up the following:
All this does is set the default max walk speed to a different value. By default, this value is 500. The SprintSpeed
variable is used here to set the max speed to 900, but you could just input 900 or whatever value you want into the set directly. Note that the Character Movement for the target here is the baseline Character Movement component each character template has.
StopSprint
looks very similar:
The WalkSpeed
variable created earlier is used to reset the character movement speed back to the original of 500.
By the way, if you want to change the default max speeds, you can click on Class Defaults
at the top of the BP_ThirdPersonCharacter
blueprint and filter in the details
for walk speed
. Just make sure you update your sprint and walk speeds accordingly.
Setting up the HUD
Building upon the work I did in my Player Stats & Hud post, I just copied the mana bar, renamed it to Stamina Bar
and placed it along with the others in my HUD blueprint.
Similarly, I went to my BPC_PlayerStats
file and added a new Set Stamina in HUD
function based off the Health and Mana ones I’d already created. I did remove the number display in favor of just using a filling/depleting bar style HUD for now.
Set up stamina drain
Moving back to the BP_ThirdPersonCharacter
function, I went to the Event Graph
to add the Stamina drain function. Starting with a CustomEvent
I named Drain Stamina
, it turned into the below:
For the stamina functions, I decided to color code it to make it easier to find it as the number of functions in the event graph grows. So this takes the current stamina, adds a value passed in and uses this neat Clamp
node that enforces max and minimum values, saving me a lot of the work I’d put into my functions in the Player Stats and Hud post. So once the value is set, it will show the value in the Hud, then check if the current stamina is 0. If it is, it stops sprinting. If its not, it checks if you’re currently sprinting and if true has a small delay before doing a recursive Drain Stamina call.
Set up stamina regen
In the same place, I also set up the stamina regen function.
This one adds 1 to the current stamina up until the max value is hit. I left the value configurable but figured it didn’t need to be its own variable for now. Once set, it updates the HUD and if the current stamina is not equal to the max stamina, and the player is neither jumping nor sprinting, it will call the function again, looping until stamina is capped.
Binding it all to a button
To wrap up the Sprint functionality, still within the BP_ThirdPersonCharacter
Event Graph, I bound the StartSprint
and StopSprint
functions to Left Shift
.
With that, sprinting in game will drain stamina over time, and once I stop running, stamina regens. Nice.
One more thing…
For the heck of it, I decided to also add a stamina drain to jumping. So I added a few things to the default Jump in the Event Graph
.
Fairly straightforward. I made the isJumping boolean toggled as it need to be. Jumping causes stamina drain and both can trigger regen.
So there we have a basic stamina bar and drain, tied to a custom sprint and added to the baseline jump.