Rate of stamina decay depanding on player speed

The title sais it all. Im wondering how i can create a stamina system that depleats faster as the player speeds up and hits max speed. And regenerates faster as the player speed decreases. And regenerates the fastes when player is stationary.

A simple way would be to have a timer that calls a function that checks the speed and adds/subtracts from the stamina value based on it. You could use a FloatCurve asset if you want a non linear effect and use the speed as “timeIn” and the value out would be the stamina loss/gain.

Depending on your game you might not need a timer if you can use the player input to sample set speeds (like w does instant walking or shift+w is instant running while no input is still). Just depends on your movement system.

Be sure to check for falling/airtime to affect stamina properly.