Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Can't disable sprinting when out of stamina

Discussion in 'Scripting' started by TheJumpah, Nov 14, 2019.

  1. TheJumpah

    TheJumpah

    Joined:
    Nov 2, 2019
    Posts:
    6
    Hello I'm pretty new to coding in unity but I've created a first person controller and I am trying to implement a stamina system. Regardless if the stamina has hit 0 or not my player remains in sprint and keeps the additional move speed from sprinting until I release the sprint key. If anyone could help me with this problem that would be great. This is almost my first time posting here so hopefully everything looks correct. code1.png code2.png code3.png
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    In your PlayerMovement script, you don't seem to be checking if you have the stamina to sprint or continue to sprint. Right now your if check just says that if I'm holding down the sprint button and I'm grounded, keep my movement speed set to the sprinting speed.

    You just need a way to check against your stamina, or setting a bool that is controlled by PlayerStats like "canSprint".

    Also, instead of bools for isSprinting, isCroached, etc. You might consider an enum to track what state the user is in.

    For future reference posting code with codes tags is the preferred method to getting someone to help out. (since it's your first time posting)
     
  3. TheJumpah

    TheJumpah

    Joined:
    Nov 2, 2019
    Posts:
    6
    Thank you, I actually got my character to stop sprinting when stamina hit 0 by checking for stamina in my PlayerMovement script. I am now experiencing a weird bug where if I just hold down the "sprint button" as soon as my stamina begins to regen to only 1 I get full sprint speed again. So now my goal is to put a delay between having 0 stamina and then regenerating the stamina. Should I use coroutines for that?