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

Hunger and Thirst Depletion

Discussion in 'Scripting' started by WMaysTech, Nov 15, 2014.

  1. WMaysTech

    WMaysTech

    Joined:
    Nov 15, 2014
    Posts:
    4
    I am creating a Day-Z like game... In my game you have to find food and water to survive.. I am using this code for depletion... The problem is that I have depletion to 0.1 but for some reason it's going down way to fast.. In the inspector the minimum number allowing is 1.. How can I fix this?

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var lastPositionY : float = 0f;
    4. var fallDistance : float = 0f;
    5. var player : Transform;
    6. private var controller : CharacterController;
    7.  
    8. var currentHealth : float = 100.0;
    9. var maxHealth : int = 100;
    10. var currentThirst : float = 100.0;
    11. var maxThirst : int = 100;
    12. var currentHunger : float = 100.0;
    13. var maxHunger : int = 100;
    14. private var barLength = 0.0;
    15. //FALL RATES (Higher = Slower)
    16. var thirstFallRate : int = 0.1;
    17. var hungerFallRate    : int = 0.1;
    18.  
    19. function Start()
    20. {
    21.     barLength =  180;
    22.     controller = GameObject.Find("First Person Controller").GetComponent(CharacterController);
    23. }
    24. function Update()
    25. {
    26. if(lastPositionY > player.transform.position.y)
    27.     {
    28.         fallDistance += lastPositionY - player.transform.position.y;
    29.     }
    30.     lastPositionY = player.transform.position.y;
    31.     if(fallDistance >= 5 && controller.isGrounded)
    32.     {
    33.         currentHealth -= 50;
    34.         ApplyNormal();
    35.     }
    36.      if(fallDistance <= 5 && controller.isGrounded)
    37.     {
    38.         ApplyNormal();
    39.     }
    40.     if(currentHealth <= 0)
    41.     {
    42.         CharacterDeath();
    43.     }
    44.     if(currentThirst >= 0)
    45.     {
    46.         currentThirst -= Time.deltaTime / thirstFallRate;
    47.     }
    48.     if(currentThirst <= 0)
    49.     {
    50.         currentThirst = 0;
    51.     }
    52.     if(currentThirst >= maxThirst)
    53.     {
    54.         currentThirst = maxThirst;
    55.     }
    56.         if(currentHunger >= 0)
    57.     {
    58.         currentHunger -= Time.deltaTime / hungerFallRate;
    59.     }
    60.     if(currentHunger <= 0)
    61.     {
    62.         currentHunger = 0;
    63.     }
    64.     if(currentHunger >= maxHunger)
    65.     {
    66.         currentHunger = maxHunger;
    67.     }
    68.     if(currentHunger <= 0 && (currentThirst <= 0))
    69.     {
    70.         currentHealth -= Time.deltaTime / 2;
    71.     }
    72.     else
    73.     {
    74.         if(currentHunger <= 0 || currentThirst <= 0)
    75.         {
    76.             currentHealth -= Time.deltaTime / 4;
    77.         }
    78.     }
    79. }
    80. function CharacterDeath()
    81. {
    82.     //game over
    83. }
    84. function OnGUI()
    85. {
    86.     GUI.Box(new Rect(55, 30, barLength, 23), "Health ---->" + "   " + currentHealth.ToString("0") + "/" + maxHealth);
    87.     GUI.Box(new Rect(55, 55, barLength, 23), "Thirst ---->" + "   " + currentThirst.ToString("0") + "/" + maxThirst);
    88.     GUI.Box(new Rect(55, 80, barLength, 23), "Hunger ---->" + "   " + currentHunger.ToString("0") + "/" + maxHunger);
    89.    
    90. }
    91. function ApplyNormal()
    92. {
    93.         fallDistance = 0;
    94.         lastPositionY = 0;
    95. }
     
  2. slay_mithos

    slay_mithos

    Joined:
    Nov 5, 2014
    Posts:
    130
    http://docs.unity3d.com/ScriptReference/Time-deltaTime.html
    This is really important, so let's try to explain it correctly.

    So, Time.deltaTime tells you the fraction of a second since the last frame.
    At a stable 60FPS, it would mean that it should be roughly 1/60.

    Now, on the page I linked, it is said to multiply this by the number you want to have "per second", in your case 0.1.

    Where you fall short is that you divided by that number, meaning that each frame, which means you are multiplying by 10 instead, so your hunger will drop at a rate of 10 per second.

    just change your division into a multiplication, and all will be fine.


    By the way, just in case, I think your health won't decrease when you "starve", because you are dividing a small number by a larger one that is an int.
    If you want it to have any effect, a division should be done using floats or doubles, or it will lead you to weird results.

    Then again, here too I suspect that you wanted a multiplication instead.


    I hope this helps a bit, feel free to say if I wasn't clear enough.
     
  3. WMaysTech

    WMaysTech

    Joined:
    Nov 15, 2014
    Posts:
    4
    Yes that helped a lot but I am very new to this... Could you please provide the code to fix this? or tell me what to do? thanks!!
     
  4. slay_mithos

    slay_mithos

    Joined:
    Nov 5, 2014
    Posts:
    130
    Pretty easy actually, and you had everything in my sentences, but for completion's sake:
    Code (CSharp):
    1.         //Line 46, change from:
    2.         currentThirst -= Time.deltaTime / thirstFallRate;
    3.         //To:
    4.         currentThirst -= Time.deltaTime * thirstFallRate;
    5.        
    6.         //Line 58:
    7.         currentHunger -= Time.deltaTime / hungerFallRate;
    8.         //To:
    9.         currentHunger -= Time.deltaTime * hungerFallRate;
    10.        
    11.         //Line 70:
    12.         currentHealth -= Time.deltaTime / 2;
    13.         //To:
    14.         currentHealth -= Time.deltaTime * 2;
    15.        
    16.         //Line 76:
    17.         currentHealth -= Time.deltaTime / 4;
    18.         //To:
    19.         currentHealth -= Time.deltaTime * 4;
    As you can see, it's just about changing divisions into multiplications, nothing overly complex, and overall much, much easier than what you did to have all that code in the first place.

    The basic is extremely easy:

    "If you want something to change at a rate of X per second, you just multiply Time.deltaTime by X".
    That X is 10 in the page I linked, 0.1, 2 and 4 for you.
     
  5. WMaysTech

    WMaysTech

    Joined:
    Nov 15, 2014
    Posts:
    4
    Ok I understand but the time is still going down 1 every second... I would like it to go down 1 every 1 minute?
     
  6. slay_mithos

    slay_mithos

    Joined:
    Nov 5, 2014
    Posts:
    130
    Well, it might be because of a flaw I completely missed:
    Code (JavaScript):
    1.  
    2. //FALL RATES (Higher = Slower)
    3. var thirstFallRate : int = 0.1;
    4. var hungerFallRate    : int = 0.1;
    You are putting them as int, which means only entire number, and are trying to put 0.1 into it, that is a float, so I assume it ceils it to 1 somehow.

    Put these as float and all should be better.
     
    WMaysTech likes this.
  7. WMaysTech

    WMaysTech

    Joined:
    Nov 15, 2014
    Posts:
    4
    Thanks!! It's fixed now