Search Unity

Stamina doesn't decrease

Discussion in 'Scripting' started by kwury, Jul 2, 2020.

  1. kwury

    kwury

    Joined:
    Jul 2, 2020
    Posts:
    2
    Hello,

    I was wondering if someone could help me with a script problem. My stamina doesn't work whenever I'm running, the stamina is supposed to decrease.
    My PlayerStats:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class PlayerStats : MonoBehaviour {
    7.  
    8.     [SerializeField]
    9.     private Image health_Stats, stamina_Stats;
    10.  
    11.     public void Display_HealthStats(float healthValue) {
    12.  
    13.         healthValue /= 100f;
    14.  
    15.         health_Stats.fillAmount = healthValue;
    16.  
    17.     }
    18.  
    19.     public void Display_StaminaStats(float staminaValue) {
    20.  
    21.         staminaValue /= 100f;
    22.  
    23.         stamina_Stats.fillAmount = staminaValue;
    24.  
    25.     }
    26.  
    27.  
    28. }
    My HealthScript:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5.  
    6. public class HealthScript : MonoBehaviour {
    7.  
    8.     private EnemyAnimator enemy_Anim;
    9.     private NavMeshAgent navAgent;
    10.     private EnemyController enemy_Controller;
    11.  
    12.     public float health = 100f;
    13.  
    14.     public bool is_Player, is_Boar, is_Cannibal;
    15.  
    16.     private bool is_Dead;
    17.  
    18.     private EnemyAudio enemyAudio;
    19.  
    20.     private PlayerStats player_Stats;
    21.  
    22.     void Awake () {
    23.        
    24.         if(is_Boar || is_Cannibal) {
    25.             enemy_Anim = GetComponent<EnemyAnimator>();
    26.             enemy_Controller = GetComponent<EnemyController>();
    27.             navAgent = GetComponent<NavMeshAgent>();
    28.  
    29.             // enemy audio in inspector toevoegen
    30.             enemyAudio = GetComponentInChildren<EnemyAudio>();
    31.         }
    32.  
    33.         if(is_Player) { //is_Player aanvinken in inspector
    34.             player_Stats = GetComponent<PlayerStats>();
    35.         }
    36.  
    37.     }
    38.    
    39.     public void ApplyDamage(float damage) {
    40.  
    41.        
    42.         if (is_Dead)
    43.             return;
    44.  
    45.         health -= damage;
    46.  
    47.         if(is_Player) {
    48.            
    49.             player_Stats.Display_HealthStats(health);
    50.         }
    51.  
    52.         if(is_Boar || is_Cannibal) { //is_Cannibal aanvinken in inspector
    53.             if(enemy_Controller.Enemy_State == EnemyState.PATROL) {
    54.                 enemy_Controller.chase_Distance = 50f;
    55.             }
    56.         }
    57.  
    58.         if(health <= 0f) {
    59.  
    60.             PlayerDied();
    61.  
    62.             is_Dead = true;
    63.         }
    64.  
    65.     }
    66.  
    67.     void PlayerDied() {
    68.  
    69.         if(is_Cannibal) {
    70.  
    71.             GetComponent<Animator>().enabled = false;
    72.             GetComponent<BoxCollider>().isTrigger = false;
    73.             GetComponent<Rigidbody>().AddTorque(-transform.forward * 5f);
    74.  
    75.             enemy_Controller.enabled = false;
    76.             navAgent.enabled = false;
    77.             enemy_Anim.enabled = false;
    78.  
    79.             StartCoroutine(DeadSound());
    80.  
    81.             // EnemyManager spawn more enemies
    82.             EnemyManager.instance.EnemyDied(true);
    83.         }
    84.  
    85.         if(is_Boar) {
    86.  
    87.             navAgent.velocity = Vector3.zero;
    88.             navAgent.isStopped = true;
    89.             enemy_Controller.enabled = false;
    90.  
    91.             enemy_Anim.Dead();
    92.  
    93.             StartCoroutine(DeadSound());
    94.  
    95.             // EnemyManager spawn more enemies
    96.             EnemyManager.instance.EnemyDied(false);
    97.         }
    98.  
    99.         if(is_Player) {
    100.  
    101.             GameObject[] enemies = GameObject.FindGameObjectsWithTag(Tags.ENEMY_TAG);
    102.  
    103.             for (int i = 0; i < enemies.Length; i++) {
    104.                 enemies[i].GetComponent<EnemyController>().enabled = false;
    105.             }
    106.  
    107.             EnemyManager.instance.StopSpawning();
    108.  
    109.             GetComponent<PlayerMovement>().enabled = false;
    110.             GetComponent<PlayerAttack>().enabled = false;
    111.             GetComponent<WeaponManager>().GetCurrentSelectedWeapon().gameObject.SetActive(false);
    112.  
    113.         }
    114.  
    115.         if(tag == Tags.PLAYER_TAG) {
    116.  
    117.             Invoke("RestartGame", 3f);
    118.  
    119.         } else {
    120.  
    121.             Invoke("TurnOffGameObject", 3f);
    122.  
    123.         }
    124.  
    125.     } // player is dood
    126.  
    127.     void RestartGame() {
    128.         UnityEngine.SceneManagement.SceneManager.LoadScene("SampleScene");
    129.     }
    130.  
    131.     void TurnOffGameObject() {
    132.         gameObject.SetActive(false);
    133.     }
    134.  
    135.     IEnumerator DeadSound() {
    136.         yield return new WaitForSeconds(0.3f);
    137.         enemyAudio.Play_DeadSound();
    138.     }
    139.  
    140. }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    I see you are making a call to Display_HealthStats when you take damage, which is great, but I don't see you ever calling Display_StaminaStats. But then, the second script you show is a health script, so I'm not sure that I'd expect changes to Stamina to be made from that script.

    Where do you call Display_StaminaStats from?
     
  3. kwury

    kwury

    Joined:
    Jul 2, 2020
    Posts:
    2
    Hi,
    If I'm correct the Display_Stats is called in my sprintandcrouch script and the stamina is supposed to decrease when i hold Left shift and walk/run i think. The thing is it doesn't work. It might be tied to my other problem: pressing or holding buttons to shoot or run etc doesnt work.

    The sprint code is:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerSprintAndCrouch : MonoBehaviour
    6. {
    7.  
    8.     private PlayerMovement playerMovement;
    9.  
    10.     public float sprint_Speed = 10f;
    11.     public float move_Speed = 5f;
    12.     public float crouch_Speed = 2f;
    13.  
    14.     private Transform look_Root;
    15.     private float stand_Height = 1.6f;
    16.     private float crouch_Height = 1f;
    17.  
    18.     private bool is_Crouching;
    19.  
    20.     private PlayerFootsteps player_Footsteps;
    21.  
    22.     private float sprint_Volume = 1f;
    23.     private float crouch_Volume = 0.1f;
    24.     private float walk_Volume_Min = 0.2f, walk_Volume_Max = 0.6f;
    25.  
    26.     private float walk_Step_Distance = 0.4f;
    27.     private float sprint_Step_Distance = 0.25f;
    28.     private float crouch_Step_Distance = 0.5f;
    29.  
    30.     private PlayerStats player_Stats;
    31.  
    32.     private float sprint_Value = 100f;
    33.     public float sprint_Treshold = 10f;
    34.  
    35.     void Awake()
    36.     {
    37.  
    38.         playerMovement = GetComponent<PlayerMovement>();
    39.  
    40.         look_Root = transform.GetChild(0);
    41.  
    42.         player_Footsteps = GetComponentInChildren<PlayerFootsteps>();
    43.  
    44.         player_Stats = GetComponent<PlayerStats>();
    45.  
    46.     }
    47.     void Start()
    48.     {
    49.         player_Footsteps.volume_Min = walk_Volume_Min;
    50.         player_Footsteps.volume_Max = walk_Volume_Max;
    51.         player_Footsteps.step_Distance = walk_Step_Distance;
    52.     }
    53.  
    54.     // Update is called once per frame
    55.     void Update()
    56.     {
    57.         Sprint();
    58.         Crouch();
    59.     }
    60.  
    61.     void Sprint()
    62.     {
    63.  
    64.         // if we have stamina we can sprint
    65.         if (sprint_Value > 0f)
    66.         {
    67.  
    68.             if (Input.GetKeyDown(KeyCode.LeftShift) && !is_Crouching)
    69.             {
    70.  
    71.                 playerMovement.speed = sprint_Speed;
    72.  
    73.                 player_Footsteps.step_Distance = sprint_Step_Distance;
    74.                 player_Footsteps.volume_Min = sprint_Volume;
    75.                 player_Footsteps.volume_Max = sprint_Volume;
    76.  
    77.             }
    78.  
    79.         }
    80.  
    81.         if (Input.GetKeyUp(KeyCode.LeftShift) && !is_Crouching)
    82.         {
    83.  
    84.             playerMovement.speed = move_Speed;
    85.  
    86.             player_Footsteps.step_Distance = walk_Step_Distance;
    87.             player_Footsteps.volume_Min = walk_Volume_Min;
    88.             player_Footsteps.volume_Max = walk_Volume_Max;
    89.  
    90.         }
    91.  
    92.         if (Input.GetKey(KeyCode.LeftShift) && !is_Crouching)
    93.         {
    94.  
    95.             sprint_Value -= sprint_Treshold * Time.deltaTime;
    96.  
    97.             if (sprint_Value <= 0f)
    98.             {
    99.  
    100.                 sprint_Value = 0f;
    101.  
    102.                 // reset the speed and sound
    103.                 playerMovement.speed = move_Speed;
    104.                 player_Footsteps.step_Distance = walk_Step_Distance;
    105.                 player_Footsteps.volume_Min = walk_Volume_Min;
    106.                 player_Footsteps.volume_Max = walk_Volume_Max;
    107.  
    108.  
    109.             }
    110.  
    111.             player_Stats.Display_StaminaStats(sprint_Value);
    112.  
    113.         }
    114.         else
    115.         {
    116.  
    117.             if (sprint_Value != 100f)
    118.             {
    119.  
    120.                 sprint_Value += (sprint_Treshold / 2f) * Time.deltaTime;
    121.  
    122.                 player_Stats.Display_StaminaStats(sprint_Value);
    123.  
    124.                 if (sprint_Value > 100f)
    125.                 {
    126.                     sprint_Value = 100f;
    127.                 }
    128.  
    129.             }
    130.  
    131.         }
    132.  
    133.  
    134.     } // sprint
    135.  
    136.     void Crouch()
    137.     {
    138.  
    139.         if (Input.GetKeyDown(KeyCode.C))
    140.         {
    141.  
    142.             // if we are crouching - stand up
    143.             if (is_Crouching)
    144.             {
    145.  
    146.                 look_Root.localPosition = new Vector3(0f, stand_Height, 0f);
    147.                 playerMovement.speed = move_Speed;
    148.  
    149.                 player_Footsteps.step_Distance = walk_Step_Distance;
    150.                 player_Footsteps.volume_Min = walk_Volume_Min;
    151.                 player_Footsteps.volume_Max = walk_Volume_Max;
    152.  
    153.                 is_Crouching = false;
    154.  
    155.             }
    156.             else
    157.             {
    158.                 // if we are not crouching - crouch
    159.  
    160.                 look_Root.localPosition = new Vector3(0f, crouch_Height, 0f);
    161.                 playerMovement.speed = crouch_Speed;
    162.  
    163.                 player_Footsteps.step_Distance = crouch_Step_Distance;
    164.                 player_Footsteps.volume_Min = crouch_Volume;
    165.                 player_Footsteps.volume_Max = crouch_Volume;
    166.  
    167.                 is_Crouching = true;
    168.  
    169.             }
    170.  
    171.         } // if we press c
    172.  
    173.  
    174.     } // crouch
    175.  
    176. } // class
    177.  
     
    Last edited: Jul 3, 2020
  4. Elango

    Elango

    Joined:
    Jan 27, 2016
    Posts:
    107
    The code should work. Are you sure stamina_Stats Image is set to Filled type?
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    I would suggest adding some Debug.Log statements through your sprint code and see if it's executing the parts you think it should. Then add another to your Display_StaminaStat method and see if that is executing. If your debug messages print out, then at least you know the code is running. But wouldn't hurt to check out what @Elango mentioned first as that would be a quick check.