I would like to add a sprinting feature in which the character can sprint for a max of 6 seconds and drain “energy”. “Energy” (being the duration of the sprint) would lower while using sprint and would slowly regenerate after sprinting. If anyone uses a sprint function like this I would really appreciate it if I could borrow or modify it for my game. Thank You!
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.
You could probably get away with a simple timeline and a float track. Have a variable for “energy” can start at 100. Create a timeline, and make a float track that starts at 100 at time 0 and goes to 0 at time 6 seconds. Off of the “update” execution pin have it continuously set the “energy” variable to the value of the float track. Have an “on pressed” event start the timeline, and “on release” hook into “reverse” this way on release “energy” will increase back up to the maximum value. Would solve all of your game mechanics pretty simply that way with only 1 variable,