I’ve come across an oddity with Time.deltaTime, specifically with accumulating time using deltaTime. For instance if I want to keep my own timer object to keep track of how long an object has been alive or in a certain state, I can simply do something like the following:
void Update ()
{
timeAlive += UnityEngine.Time.deltaTime;
}
The strange part comes in if you do this type of calc and let it run for a long time, specifically for me if I run it for longer than 68 minutes, I begin to see some odd changes in how fast timeAlive increases. After this period of time, deltaTime becomes larger than it should be.
To test, I wrote the following component:
using UnityEngine;
using System.Collections;
public class TestUnityTime : MonoBehaviour
{
void Start ()
{
Debug.Log("Clock is high res? "
+ System.Diagnostics.Stopwatch.IsHighResolution);
Debug.Log("StopWatchTime, Time, RealTimeSinceStartup, "
+ "UpdateAccumulation, FixedUpdateAccumulation");
stopWatch = new System.Diagnostics.Stopwatch();
stopWatch.Start();
accumulatedDelta = 0;
accumulatedDeltaFixed = 0;
StartCoroutine(Print());
}
void Update ()
{
accumulatedDelta += UnityEngine.Time.deltaTime;
}
void FixedUpdate()
{
accumulatedDeltaFixed += UnityEngine.Time.fixedDeltaTime;
}
IEnumerator Print()
{
while (true)
{
double swSeconds = stopWatch.Elapsed.TotalSeconds;
Debug.Log(swSeconds
+ ", " + (UnityEngine.Time.time - swSeconds)
+ ", " + (UnityEngine.Time.realtimeSinceStartup - swSeconds)
+ ", " + (accumulatedDelta - swSeconds)
+ ", " + (accumulatedDeltaFixed - swSeconds));
yield return new WaitForSeconds(printInterval);
}
}
public float printInterval = 60; // interval for sampling and printing deltas
private System.Diagnostics.Stopwatch stopWatch;
private float accumulatedDelta;
private float accumulatedDeltaFixed;
}
So essentially this accumulates the time that the app has been running using independent methods: deltaTime, fixedDeltaTime, and a StopWatch. It then periodically logs the difference from the stop watch to each of these other independent accumulations, since we can presume that the stop watch is fairly accurate. Assuming that deltaTime is truly frame independent, while accumulated time might be off from the stop watch slightly, that delta should remain fairly constant over time. Unfortunately, this does not seem to be the case, and I’m not sure why.
Here are the results of running this for just a little over 8 hours:
Figure 1:
And here is the [20397-unitytimetest.txt|20397]
I also ran a similar test calculating my own time delta based on Time.time, purely to ensure that the issue was not caused by floating point accuracy, or rounding errors within the script. To do this, inside of update I added the following:
customDelta = Time.time - timeLastFrame;
accumulatedCustomDelta += customDelta;
timeLastFrame = Time.time;
This custom delta is shown by the blue line in Figure 2.
Figure 2:
![Accumulated Time Drift with Custom Delta] 3
The interesting thing is that somewhere between 4080-4140 seconds, deltaTime becomes much larger than it should be, causing the accumulated time to become increasingly faster than the stop watch. This also changes between 8161-8221 seconds, and 16322-16442 seconds. Sometimes slowing down, sometimes speeding up.
fixedDeltaTime also seems to become smaller than it should around the 8161-8221 second mark. It perhaps may shift and become larger if run for longer than the 8 hours, not sure.
Time.time and realTimeSinceStartup are both solid. They’re slightly off from stop watch, which is to be expected simply due to the timing of when these components start, but they stay fairly constant.
So my questions:
- Why does the rate of change of this accumulated deltaTime change so drastically over time?
- Why doesn’t the accumulation of Time.deltaTime come anywhere close to Time.time since Time.time should itself be an accumulation of deltaTime?
I’m using Unity 4.3.1f1, but I’ve also tested on 3.5.6f4 with similar results.