Currently, the movement speed is quite slow without an option to sprint like most First-Person Shooters. I found an online tutorial on YouTube that demonstrates how to implement the Stamina and Sprinting element.
The first step was to create an input binding. I assigned the Left Shift key to the Sprint, which is commonly used in online games.
I remembered about creating a health and armour bar previously, using the same principle I created a bar for stamina. Undecided whether I will keep this in the final product, but I have added it in for the time being for the sole purpose of ensuring that the stamina system is functioning as expected.
I had to create another Binding for the stamina bar. Following exactly the same set-up as the Health and Armour bars except for one step.
By including the Division Integer we can divide our value by 100. The reason for this is because it is easier for us to operate on a 0-100 scale, but the Progress Bar actually operates on a scale of 0-1. By dividing it by 100 ensures that the value is correct on both ends.
In the FirstPerson Character Blueprint file, I created two more variables: Stamina (integer) and IsSprinting (Boolean). I set the default value of Stamina to 100 - meaning we will spawn with a full stamina bar.
- Firstly we check to see if the Sprint key has been pressed (Left Shift).
- If the Sprint key is pressed, we check whether the Stamina is greater than or equal to 1.
- If True, set the IsSprinting boolean to true, and set the Max Walk Speed to 850.
- If False, do nothing.
- If the Sprint key is released, set the IsSprinting boolean to false.
- Then set the Max Walk Speed to 600.
This is basic functionality for sprinting. However, to add a sense of realism to the game I would like to have a limit on how much the player can sprint - a regeneration system.
From the tutorial, I was able to learn that we use the Event Tick - which is an event called on every frame of gameplay. We use the delays to adjust how frequently they are called. If we didn't use the delay on the regeneration as an example, the stamina bar would refill instantly the moment the player releases the sprint key - which would practically defeat the purpose of the stamina system.
- We check to see if the player is currently sprinting
- If True, check to see if the Stamina is less than or equal to 0
- If True, set IsSprinting to false and set Max Walk Speed to 600 (Walking speed)
- If False, set Stamina to (Stamina value - 1).
- If IsSprinting is False we set the stamina to (Stamina value + 1)
This provides the functionality that is required. However, there are two problems with this that are not covered in the tutorial.
- The Stamina does not have a minimum and maximum value. It will continue to recharge past 100, or drop below 0.
- The Stamina reduces if the player holds down the Left Shift key, even while they are not moving.
After a while of searching for how to prevent a player's Stamina reducing while not moving, I came across the question being asked before: how-to-detect-player-movement
This lead to me being able to create a method of checking whether a player is moving, before executing the Sprinting. By performing this, we are checking whether they are moving on the X or Y axis, and if they are we can reduce the Stamina. If they are not moving, we begin regenerating.
Now that I have managed to fix the two issues, I feel like the stamina system is perfected. The only questionable aspect is the UI. In a lot of games, the stamina system exists, but there is no indication bar to tell you - rather they indicate it by audio (breathing SFX). The UI currently is very basic and ugly, I intend to change this later on but question whether the stamina is necessary.









No comments:
Post a Comment