I’ve not got an example, but the easiest way of doing this (without thinking too much about it):
- Float variable ‘energy’ which should default to some value (just say 100 for now)
- Float variable ‘regenerationSpeed’ which should say how much energy is recovered every second when not sprinting
- Float variable ‘drainSpeed’ which should say how much energy is lost when spriting
- Button that listens for the OnPressed and OnReleased events
- OnPressed will which if ‘energy’ is > 0 and if so, set a bool bIsSprinting
- OnReleased will unset bIsSprinting
- Your Tick event should check if bIsSprinting is set and ‘energy’ > 0. If it is then set the character speed (can set this elsewhere for better efficiency, but this will do for now). Also set energy = energy - (drainSpeed * DeltaTime). This will drain your energy at the rate specified regardless of FPS
- If your tick event sees that bIsSprinting is set and energy <= 0 then bIsSprinting should be unset
- If Your tick event sees bIsSprinting is not set then you should set energy = energy + (regenerationSpeed * DeltaTime). If energy > your maximum value of energy, then just set energy = maximum value.
That should hopefully get you to a good starting place.