Search Unity

Sprinting With Stamina

Discussion in 'Scripting' started by AnimusRex, Oct 9, 2019.

  1. AnimusRex

    AnimusRex

    Joined:
    Apr 26, 2019
    Posts:
    67
    So I have a stamina bar but it's wonk AF.

    Lets you keep sprinting when stamina is zero if you don't let go of the button, and takes a few seconds to start refilling the bar for no reason I understand.

    Code (CSharp):
    1.         if (characterController.isGrounded && Input.GetButtonDown("Sprint") && groundSlopeAngle < slopeLimit && playerStamina > 0)
    2.         {
    3.             isSprinting = true;
    4.         }
    5.  
    6.         if (characterController.isGrounded && Input.GetButtonUp("Sprint") || playerStamina == 0)
    7.         {
    8.             isSprinting = false;
    9.         }
    10.  
    11.         if (isSprinting)
    12.         {
    13.             TakeStamina(0.5f);
    14.             movementSpeed = sprintSpeed;
    15.         }
    16.  
    17.         if (!isSprinting)
    18.         {
    19.             movementSpeed = walkSpeed;
    20.  
    21.             if (playerStamina < 100)
    22.             {
    23.                 AddStamina(0.4f);
    24.             }
    25.             if (playerStamina >= 100)
    26.             {
    27.                 playerStamina = 100;
    28.                 sliderStamina.value = playerStamina;
    29.             }
    30.         }
    31.  
    32.         MoveDir = (desiredMoveDirection * movementSpeed);
    33.         MoveDir.y = ySpeed;
    34.         jumpDelay -= Time.deltaTime;
    35.     }
    36.  
    37.     public void TakeStamina(float amount)
    38.     {
    39.         playerStamina -= amount;
    40.         sliderStamina.value = playerStamina;
    41.     }
    42.  
    43.     public void AddStamina(float amount)
    44.     {
    45.         playerStamina += amount;
    46.         sliderStamina.value = playerStamina;
    47.     }
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    A couple of notes:
    - I assume the first lines belong to Update()
    - TakeStamina and AddStamina should be moderated by Time.DeltaTime if they are invoked in Update()
    - Check your guards and use parantheses. Are you sure that the || is evaluated correctly versus the other "&&"?
    - jumpDelay, moveDir have no function. If you edit out stuff, make sure you edit out everything, but it's usually better to leave in everything.
     
    Last edited: Oct 9, 2019
    SparrowGS likes this.
  3. Deleted User

    Deleted User

    Guest

    Your playerStamina will rarely be exact 0. You should not use "==" when comparing floats.
    Better use " playerStamina < 0"

    Tip for the future. If you want to know, if a float has a given value:
    Code (CSharp):
    1. float stamina;
    2. float boarderValue;
    3. float tolerance;
    4.  
    5. if(Mathf.abs(stamina-boarderValue) < tolerance)
    6. {
    7.       //...
    8. }