Time.deltatime

Hi Everyone,

first time using this forum and github. I am trying to simulate the emptying of a tank at a fixed flowrate and I would like to show the changing/decreasing volume of the tank as time passes.

I used time.deltatime and I am multiplying that by the flow out of the tank to get volume then I am subtracting this volume from the tanks contents to show the current volume in the tank but the initial tank inventory is not changing. Not sure what I am doing wrong, so I have uploaded the Unity project on github: https://github.com/AkameC/TankInventory

Any help would be appreciated, thanks.

Hello @Aleem,

This I believe is because of the calculations being carried out when using these floating point numbers. The console for example always displayed fixed precision. I would recommend moving to decimals.

I have taken just the one script and removed lots lines that weren’t necessary for the test, if you try the following, just attached to a game object, you should see the desired output in the console;

using UnityEngine;

public class Test : MonoBehaviour
{
    decimal TankInventory;    // Tank inventory is in barrels
    decimal Throughput;   // Throughput is in barrels per day 
    decimal ThroughputPerSecondInBarrels;

    // Use this for initialization
    void Start()
    {
        // initialise
        TankInventory = 720000;
        Throughput = 150000;

        ThroughputPerSecondInBarrels = Throughput / (60 * 60 * 24); // calculated once
    }

    // Update is called once per frame
    void Update()
    {

        CalculateInventory();
    }

    void CalculateInventory()
    {
        float timer = Time.deltaTime; // time in seconds since last frame
        decimal deduction = (decimal)timer * ThroughputPerSecondInBarrels;

        TankInventory -= deduction;
        Debug.Log("Tank : " + TankInventory.ToString("F4"));
        Debug.Log("Deduction : " + deduction.ToString("F4"));
    }
}

Note: The “F4” is just for the .ToString() method, so that it shortens the output to the console. When you output to your UI text object you may want to consider something similar.

1 Like

Hi Rob,

Thanks for helping with this, I had no idea that decimal existed. I was going to try to refactor the code afterwards but I am surprised at the amount of lines that you have been able to remove.

Hi @Aleem,

I wasn’t actually removing the other lines of code for any significant point, just because it was a bit easier for me to work with as I wasn’t running your whole project, only this one script, I simply dropped it on a game object and let it run. You may of course need many of the things I removed for the rest of your game, so it’s not for me to say really :slight_smile:

I did spend quite a bit of time trying to get the float to work, but what I round was that in the console, the output of TankInventory only ever appeared to one decimal place, both before and after any calculation. If I let it keep running for long enough the first decimal digit did do down, but it was going down at a rate of 0.1 initially around 1 second, and then the duration seemed to increase - it was certainly not accurate.

I then thought about the amount of time I was spending on trying to make a float work, and why it needed to be a float. Based on the objective, it didn’t really seem to matter what our data type was to some degree, as long as it could reasonably accurately show to rate of deduction.

With decimal you can of course have a large number of decimal places should you wish, and without knowing the full plan behind your goal here it would be impossible for me to start what is right, but I thought at least 4 for the demo would be enough, you could always change later on etc.

The one thing that I did change which seemed relevant was moving part of the calculation to the Start() method, as both your throughput and the number of second, minute and hours in a day were constant, there was no need to calculate that every frame - so you got a little performance boost there too :wink:

Glad to be of help anyway :slight_smile:

1 Like

Privacy & Terms