Stamina goes into negative value (down to -1) when sprinting FIX?

Hey guys (very noob dev here)

So I did a tutorial on sprinting and regenerating stamina and it all seems to be working decently. I just have two questions:

My only problem is, sometimes my stamina value goes into negative value (down to -1). Which change/s do I need to make for it to only go down to 0 stamina and never below that?

Also added to that, how would I go about adding a delay, so that when I run out of stamina, my character takes a few seconds before regenerating stamina?

Here is my sprint blueprint:

Also, here is the function for RegenSprint:

If you could be specific in what I need to do that would be great, I’ve only been working in Unreal for the last 3 weeks or so, so still very new.

Thank you for your time
Kind regards

Firstly, I congratulate you in joining Unreal Engine. Then about your problems I think these solutions may help.

  1. Preventing Negative Stamina:
  • In your sprint logic, you’re currently subtracting the stamina cost from the current stamina without checking if it would result in a negative value. To prevent the stamina from going below 0, you need to add a condition to check if the subtraction would result in a negative value, and if so, set the stamina to 0 instead.
  • In your Sprint function, after subtracting the stamina cost, add a branch node to check if the resulting stamina value would be less than 0. If it is, set the stamina to 0 instead of subtracting the cost. Here’s how you can do it:
  1. Adding Delay for Stamina Regeneration:
  • To add a delay before stamina regeneration kicks in after running out of stamina, you can use a delay node in your RegenSprint function. Simply add a delay node before setting the bIsSprinting variable to true. Here’s how you can do it:
  • Adjust the duration of the delay node according to how long you want the delay to be before stamina starts regenerating.

By implementing these changes, you should prevent your stamina from going into negative values and add a delay before stamina regeneration kicks in after running out. Let me know if you need further clarification or assistance!

1 Like