Add Stamina to Sprinting - DrowningDragons/MoveIt-Wiki GitHub Wiki

Foreword

Absolutely no replication is required for this tutorial. If you find yourself wanting to replicate something just because you're making a multiplayer game: Don't. Its all net predicted already, we're just hooking into that system

This is meant to introduce you to the concepts behind a stamina system, if you want something more involved or complex, its up to you to build that

Guide

All this work will be done in your character blueprint

Properties

  • Add four float properties and set them to the corresponding values:
    • Stamina -> 0.0
    • MaxStamina -> 100.0
    • StaminaDrainRate -> 0.4
    • StaminaFillRate -> 1.0

Event Tick

  • Setup your blueprint graph Event Tick like so:

Full Image

AddingStaminaEventTick

  • Now any time you sprint your stamina will deplete, and when you're not sprinting (or below walk speed from turning) it will refill. Of course, to visualize this you will either need to add it to your interface or at least Print String it :)

Can Sprint

  • Except, you can also sprint forever despite no stamina. Override the function "Can Sprint" and set it up as follows:

Full Image

AddingStaminaCanSprint

Final Step

  • When "On Stop Sprinting" is called, check if Stamina is zero, if so call "Stop Sprinting" - this will release the input.

Otherwise, what will happen is: Stamina reaches 0, sprinting ends, stamina reaches 0.01, sprinting starts, and the cycle repeats indefinitely.

Alternatively, make a system where stamina has to reach a certain percent before you can start again :) To do this, in 'Can Sprint', you would check if it is either ( Already sprinting OR stamina >= MinSprintStartStamina )