Search Unity

Player can still run (hold left shift) even when stamina float value is 0.0f

Discussion in 'Scripting' started by Arjay01, Mar 10, 2019.

  1. Arjay01

    Arjay01

    Joined:
    Aug 26, 2017
    Posts:
    9
    As a newbie programmer obviously this got me frustrating for awhile and couldn't find a fix. briefly, it was meant to automatically force the player to stop running and would be converted to walking while the stamina value is 0 even when I still hold shift. I tried to use a Boolean 'canSprint' and a statement includes the condition if stamina is equals to 0. but it only kept running.
    how should I fix this?

    the problem lies in the 'SprintInput()' function

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class PlayerMovement : MonoBehaviour
    7. {
    8.  
    9.     //player UI
    10.     public float Stamina;
    11.     public float staminaOverTimeUsage;
    12.     public float staminaOverTimeRegen;
    13.     public float minAmount;
    14.  
    15.     public Slider staminaBar;
    16.  
    17.     // player movement function
    18.     [SerializeField] private string horizontalInput;
    19.     [SerializeField] private string verticalInput;
    20.     [SerializeField] private float movementSpeed;
    21.  
    22.     [SerializeField] private AnimationCurve jumpFallOff;
    23.     [SerializeField] private float jumpMultiplier;
    24.  
    25.     [SerializeField] private KeyCode jumpKey;
    26.     [SerializeField] private KeyCode sprintKey;
    27.     [SerializeField] private KeyCode crouchKey;
    28.  
    29.     private CharacterController charController;
    30.  
    31.     private bool isJumping;
    32.     private bool isSprinting;
    33.     private bool canSprint;
    34.  
    35.     private bool isCrouching;
    36.     private bool canCrouch;
    37.  
    38.  
    39.  
    40.     private void Start()
    41.     {
    42.         canSprint = true;
    43.         canCrouch = true;
    44.         charController = GetComponent<CharacterController>();
    45.  
    46.         staminaBar.maxValue = Stamina;
    47.         StaminaSystem();
    48.     }
    49.  
    50.     private void Update()
    51.     {
    52.         charMovement();
    53.         StaminaSystem();
    54.     }
    55.  
    56.  
    57.     private void StaminaSystem()
    58.     {
    59.         Stamina = Mathf.Clamp(Stamina, 0, 100f);
    60.         staminaBar.value = Stamina;
    61.     }
    62.  
    63.     private void charMovement()
    64.     {
    65.         float horizInput = Input.GetAxis(horizontalInput) * movementSpeed;
    66.         float vertInput = Input.GetAxis(verticalInput) * movementSpeed;
    67.  
    68.         Vector3 forwardMovement = transform.forward * vertInput;
    69.         Vector3 rightMovement = transform.right * horizInput;
    70.  
    71.         charController.SimpleMove(forwardMovement + rightMovement);
    72.  
    73.         JumpInput();
    74.         SprintInput();
    75.         CrouchInput();
    76.     }
    77.  
    78.     private void JumpInput()
    79.     {
    80.         if (Input.GetKeyDown(jumpKey) && !isJumping)
    81.         {
    82.             isJumping = true;
    83.             StartCoroutine(JumpEvent());
    84.         }
    85.     }
    86.  
    87.     private void SprintInput()
    88.     {
    89.         if (Input.GetKeyDown(sprintKey) && canSprint == true)
    90.         {
    91.             isSprinting = true;
    92.             canCrouch = false;
    93.             movementSpeed += 5.0f;
    94.  
    95.             if (Stamina == 0)
    96.             {
    97.                 canSprint = false;
    98.             }
    99.             else
    100.             {
    101.                 canSprint = true;
    102.             }
    103.         }
    104.         else if (Input.GetKeyUp(sprintKey) && canSprint == true)
    105.         {
    106.             isSprinting = false;
    107.             canCrouch = true;
    108.             movementSpeed -= 5.0f;    
    109.         }
    110.    
    111.  
    112.         if (isSprinting == true)
    113.         {
    114.             Stamina -= staminaOverTimeUsage * Time.deltaTime;
    115.         }
    116.         else if (isSprinting == false)
    117.         {
    118.             Stamina += staminaOverTimeRegen * Time.deltaTime;
    119.         }
    120.  
    121.      
    122.         // balance stamina value at 0
    123.         if (Stamina < 0.0f)
    124.         {
    125.             Stamina = 0f;
    126.         }
    127.      
    128.         StaminaSystem();
    129.     }
    130.  
    131.     private void CrouchInput()
    132.     {
    133.  
    134.         if (Input.GetKeyDown(crouchKey) && canCrouch == true)
    135.         {
    136.             charController.height -= 1.20f;
    137.             isCrouching = true;
    138.             canSprint = false;
    139.             movementSpeed -= 4.0f;
    140.         }
    141.         else if (Input.GetKeyUp(crouchKey) && canCrouch == true)
    142.         {
    143.             charController.height += 1.20f;
    144.             isCrouching = false;
    145.             canSprint = true;
    146.             movementSpeed += 4.0f;
    147.         }
    148.     }
    149.  
    150.     private IEnumerator JumpEvent()
    151.     {
    152.         charController.slopeLimit = 90.0f;
    153.         float timeInAir = 0.0f;
    154.  
    155.         do
    156.         {
    157.             float jumpForce = jumpFallOff.Evaluate(timeInAir);
    158.             charController.Move(Vector3.up * jumpForce * jumpMultiplier * Time.deltaTime);
    159.             timeInAir += Time.deltaTime;
    160.             yield return null;
    161.         } while (!charController.isGrounded && charController.collisionFlags != CollisionFlags.Above);
    162.  
    163.         charController.slopeLimit = 45.0f;
    164.         isJumping = false;
    165.     }
    166. }
    167.  
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    GetKeyDown only happens for the frame the key is pushed. So if I push the key down, my character starts running. If I never let go, I'll never stop sprinting. Technically, your Stamina check is only happening once right when I hit the key to sprint.

    If you add a debug.log message to your if statements within SprintInput you should see it only print out once.

    https://docs.unity3d.com/ScriptReference/Input.GetKeyDown.html
     
  3. Arjay01

    Arjay01

    Joined:
    Aug 26, 2017
    Posts:
    9
    so is there another method that would still allow me to use the keycode instead of GetButton, getAxis or nothing else?
     
  4. Arjay01

    Arjay01

    Joined:
    Aug 26, 2017
    Posts:
    9
    you still left me in the dark here.
     
  5. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637