I’ve poked around to get a sprinting mechanic that lasts 5 seconds (Sprint Timer), goes on a 10 second cooldown (Sprint Cooldown Timer) and here it is:
Used variable types and names:
- float “SprintTimer”
- float “SprintCooldownTimer”
- boolean “isCoolingDown” (set to false by default)
The logic behind it assuming someone just pressed Left Shift and hasn’t sprinted beforehand:
- When Left Shift is pressed, go to the branch statement (aka: if statement)
- Check to see if the boolean “isCoolingDown” is true or false.
- Assuming that this is the first time someone is sprinting, the boolean is false by default and therefore, will sprint for 5 seconds (the 5 second delay that utilizes SprintTimer).
- Once the 5 seconds of sprinting is up, set the cooldown to true (by setting isCoolingDown to true).
- IMPORTANT: You need to make sure that the sprint properly enters cooldown, that’s why there is another delay after the isCoolingDown is set to true. After this delay (which lasts 10 seconds because that’s the length of the sprint cool down ), set isCoolingDown to false.
- Once the player lets go of the sprint button and presses it again immediately, the player can thus sprint again.
If you’re reading this, you may be wondering: “But rayDawg, why didn’t you just connect the same variables multiple times such as the character movement variable instead of creating multiple copies??”
The answer is because I wanted to make this look as clean and understandable as possible so anyone can “get it” at first glance.