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. Dismiss Notice

Question Player stats

Discussion in 'Scripting' started by nubictvgames, Jul 16, 2021.

  1. nubictvgames

    nubictvgames

    Joined:
    May 29, 2020
    Posts:
    3
    first script player stat what can be improved?

    did I implement the death system correctly?


    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.     public float Health;
    9.     public float helathOverTime;
    10.  
    11.     public float Hunger;
    12.     public float hungerOverTime;
    13.  
    14.     public float Stamina;
    15.     public float staminaOverTime;
    16.  
    17.     public float Thist;
    18.     public float thistOverTime;
    19.  
    20.  
    21.     public Slider HealthBar;
    22.     public Slider StaminaBar;
    23.     public Slider HungerBar;
    24.     public Slider ThistBar;
    25.  
    26.     public float minAccount=0f;
    27.     public float sprintSpeed=5f;
    28.  
    29. public PlayerMove controller;
    30.  
    31.  
    32.  
    33.  
    34. IEnumerator CoroutineHealth()
    35.      {
    36.  
    37. while(Health>0)
    38. {
    39.             Health-=helathOverTime*Time.deltaTime;
    40.             Debug.Log("health");
    41.             yield return null;
    42.  
    43. }
    44.      }
    45.        
    46.     // Start is called before the first frame update
    47.     void Start()
    48.     {
    49.         HealthBar.maxValue=Health;
    50.         StaminaBar.maxValue=Stamina;
    51.         HungerBar.maxValue=Hunger;
    52.         ThistBar.maxValue=Thist;
    53.         UpdateUI();
    54.        
    55.     }
    56.  
    57.     // Update is called once per frame
    58.     void Update()
    59.     {
    60.         CalculateValue();
    61.         DeadMessange();
    62.     }
    63.  
    64.     void CalculateValue()
    65.     {
    66.         Hunger-=hungerOverTime*Time.deltaTime;
    67.         Thist-=thistOverTime*Time.deltaTime;
    68.         if (Hunger<=minAccount || Thist<=minAccount)
    69.         {
    70. StartCoroutine (CoroutineHealth());
    71.  
    72.              Stamina-=staminaOverTime*Time.deltaTime;
    73.         }
    74.  
    75.         if(controller.speed>sprintSpeed)
    76.         {
    77.             Stamina-=staminaOverTime*Time.deltaTime;
    78.             Debug.Log("staminaAc");
    79.         }
    80.         else
    81.         {
    82.                         Stamina+=staminaOverTime*Time.deltaTime;
    83.  
    84.         }
    85.    
    86.      
    87.  
    88.    
    89. UpdateUI();
    90.     }
    91.     void UpdateUI()
    92.     {
    93.         Health=Mathf.Clamp(Health,0f,100f);
    94.         Hunger=Mathf.Clamp(Hunger,0f,100f);
    95.         Thist=Mathf.Clamp(Thist,0f,100f);
    96.         Stamina=Mathf.Clamp(Stamina,0f,100f);
    97.  
    98.  
    99.         HealthBar.value=Health;
    100.         HungerBar.value=Hunger;
    101.         StaminaBar.value=Stamina;
    102.         ThistBar.value=Thist;
    103.  
    104.     }
    105.     void DeadMessange()
    106. {
    107.  
    108.       //Dead
    109.         if(Health<=0)
    110.         {
    111.             Debug.Log("Dead Player");
    112.         }
    113.  
    114.        
    115. }
    116. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    How in the world could we possibly know? We're not running the above code to even see if it works in the first place, let alone works well. Only you can do that.

    If you actually have a specific question, here is how to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220
     
    Joe-Censored likes this.
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If the code does what you intended it to do, its performance is within your requirements, and the code is maintainable going forward, then the code was done "correctly".
     
  4. nubictvgames

    nubictvgames

    Joined:
    May 29, 2020
    Posts:
    3
    thank you