Hunger and Thirst Depletion

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?

#pragma strict

var lastPositionY : float = 0f;
var fallDistance : float = 0f;
var player : Transform;
private var controller : CharacterController;

var currentHealth : float = 100.0;
var maxHealth : int = 100;
var currentThirst : float = 100.0;
var maxThirst : int = 100;
var currentHunger : float = 100.0;
var maxHunger : int = 100;
private var barLength = 0.0;
//FALL RATES (Higher = Slower)
var thirstFallRate : int = 0.1;
var hungerFallRate    : int = 0.1;

function Start()
{
    barLength =  180;
    controller = GameObject.Find("First Person Controller").GetComponent(CharacterController);
}
function Update()
{
if(lastPositionY > player.transform.position.y)
    {
        fallDistance += lastPositionY - player.transform.position.y;
    }
    lastPositionY = player.transform.position.y;
    if(fallDistance >= 5 && controller.isGrounded)
    {
        currentHealth -= 50;
        ApplyNormal();
    }
     if(fallDistance <= 5 && controller.isGrounded)
    {
        ApplyNormal();
    }
    if(currentHealth <= 0)
    {
        CharacterDeath();
    }
    if(currentThirst >= 0)
    {
        currentThirst -= Time.deltaTime / thirstFallRate;
    }
    if(currentThirst <= 0)
    {
        currentThirst = 0;
    }
    if(currentThirst >= maxThirst)
    {
        currentThirst = maxThirst;
    }
        if(currentHunger >= 0)
    {
        currentHunger -= Time.deltaTime / hungerFallRate;
    }
    if(currentHunger <= 0)
    {
        currentHunger = 0;
    }
    if(currentHunger >= maxHunger)
    {
        currentHunger = maxHunger;
    }
    if(currentHunger <= 0 && (currentThirst <= 0))
    {
        currentHealth -= Time.deltaTime / 2;
    }
    else
    {
        if(currentHunger <= 0 || currentThirst <= 0)
        {
            currentHealth -= Time.deltaTime / 4;
        }
    }
}
function CharacterDeath()
{
    //game over
}
function OnGUI()
{
    GUI.Box(new Rect(55, 30, barLength, 23), "Health ---->" + "   " + currentHealth.ToString("0") + "/" + maxHealth);
    GUI.Box(new Rect(55, 55, barLength, 23), "Thirst ---->" + "   " + currentThirst.ToString("0") + "/" + maxThirst);
    GUI.Box(new Rect(55, 80, barLength, 23), "Hunger ---->" + "   " + currentHunger.ToString("0") + "/" + maxHunger);
   
}
function ApplyNormal()
{
        fallDistance = 0;
        lastPositionY = 0;
}

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.

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!!

Pretty easy actually, and you had everything in my sentences, but for completion’s sake:

        //Line 46, change from:
        currentThirst -= Time.deltaTime / thirstFallRate;
        //To:
        currentThirst -= Time.deltaTime * thirstFallRate;
       
        //Line 58:
        currentHunger -= Time.deltaTime / hungerFallRate;
        //To:
        currentHunger -= Time.deltaTime * hungerFallRate;
       
        //Line 70:
        currentHealth -= Time.deltaTime / 2;
        //To:
        currentHealth -= Time.deltaTime * 2;
       
        //Line 76:
        currentHealth -= Time.deltaTime / 4;
        //To:
        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.

Ok I understand but the time is still going down 1 every second… I would like it to go down 1 every 1 minute?

Well, it might be because of a flaw I completely missed:

//FALL RATES (Higher = Slower)
var thirstFallRate : int = 0.1;
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.

1 Like

Thanks!! It’s fixed now