Player Stamina System (Unreal Engine 5)

In the previous post I made a simple health system and while it's great that player takes damage and can die (trust me it is great), it isn't that fun if the player just runs around taking damages. So, a stamina system is needed. 

First, I set the stamina system to a left shift key event, This could have been done as an input action event but I wanted to approach this system differently to how I have done things in the past. A variable for  checking if the player is running (pressing the left shift key) was created and this changed whether the walk speed was at the maximum sprint speed, or the default walking speed if the left shift key wasn't pressed. For the events after the re-triggerable delays, new variables were needed because being able to just sprint forever isn't really fun.


First up was the Use/Drain Stamina system. I set up a variable for the stamina and set the value to 100 and set a rule to subtract that number by 2. Then a branch was set up to set a condition that if the stamina was more than zero, then it would print the current stamina value. If the value had hit zero, then the player would back to walking. Back to the original system, the re-triggerable delay goes to a branch that checks if stamina can be used and if it can, it does the loop again. 

As it stands, we can now sprint and the bar value will deplete until it hits zero and the player can no longer sprint, but never being able to sprint again, isn't much fun.


The next addition to the system, was to allow the stamina value to go back up to 100. This was a simple system to implement as all I needed to do was check whether the value of the stamina was equal, or greater, than 100. If it was then we cannot increase the stamina anymore. It it is less than 100, then the value would increase by 2 until it hit 100. A variable was set as a bool. As before, that event could be called in the main stamina block. The code worked exactly the same as Use Stamina, have a re-triggerable delay and make sure the vent checks if the Increase Stamina variable is active, then redo the loop.



The Player_UI widget already displayed the health bar, I added a stamina bar underneath.
 

Finally, I modified the stamina bar to update based on the character stamina. And the video below will show that system working. When left shift is pressed the stamina bar drains and when the key is not pressed, stamina is regained. There is a small bug present where the stamina increase and decrease are conflicting, this is a basic implementation and the numbers will be adjusted as the project requires.





Comments

Popular posts from this blog

Creating a simple dialogue system in Unreal Engine 5

Remaking Mario in Unreal Engine 5 (Part 2)

Remaking Mario in Unreal Engine 5 (Part 1)