Search Unity

Trouble getting hunger/thirst to act right.

Discussion in 'Scripting' started by Proposed, Jan 17, 2022.

  1. Proposed

    Proposed

    Joined:
    Jan 16, 2022
    Posts:
    1
    I'm having trouble with my code below. I think it has something to do with the division and size of integers. I want the rate to be 7200 but it almost puts it at a standstill where the values do not update. I can also multiply it by 1.388888888888889e-4 (I can simplify this number). Each way makes the values essentially stop updating all together. If I set the rate to 2000 the values update. Not sure what I'm doing wrong.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class playerAttributes: MonoBehaviour
    7. {
    8.     //Variables
    9.     public float maxHealth, maxThirst, maxHunger;
    10.     public float thirstRate, hungerRate;
    11.     public float health, thirst, hunger;
    12.     public Slider healthSlider, thirstSlider, hungerSlider;
    13.     public bool dead;
    14.  
    15.     //Functions
    16.     public void Start()
    17.     {
    18.         health = maxHealth;
    19.         hunger = maxHunger;
    20.         thirst = maxThirst;
    21.     }
    22.  
    23.     //Check and Update players current stats
    24.     public void Update()
    25.     {
    26.         if (!dead)
    27.         {
    28.             thirstIncrease();
    29.             hungerIncrease();
    30.             if (thirst <= 0)
    31.                 damageTick();
    32.             if (hunger <= 0)
    33.                 damageTick();
    34.         }
    35.         if (health <= 0)
    36.             die();
    37.         hungerSlider.value = hunger;
    38.         healthSlider.value = health;
    39.         thirstSlider.value = thirst;
    40.  
    41.  
    42.     }
    43.  
    44.     //Increase how thirsty the player is by defined thirst rate.
    45.     public void thirstIncrease()
    46.     {
    47.             thirst -= Time.deltaTime / thirstRate;
    48.     }
    49.  
    50.     //Increase how hungry the player is by defind hunger rate.
    51.     public void hungerIncrease()
    52.     {
    53.             hunger -= Time.deltaTime / hungerRate;
    54.     }
    55.  
    56.     //Player died. Print message and eventually allow respawn.
    57.     public void die()
    58.     {
    59.         dead = true;
    60.         print("You have died because of thirst, hunger or was killed by something else...");
    61.     }
    62.  
    63.     //Damage tick for hunger or thirst.
    64.     public void damageTick()
    65.     {
    66.         health -= Time.deltaTime / 5;
    67.  
    68.     }
    69.  
    70. }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Why are you dividing by the hunger/thirst rate. Shouldn't you be multiplying?
     
    orionsyndrome and RadRedPanda like this.