- Home /
Time.deltaTime not consistent over time
I am trying to do network time synchronization across clients, but I noticed some weird behavior and setup a simple project to test Time.deltaTime as it counted up. All I do is have a variable in the project that starts at 0 and holds the milliseconds since the program started. I display this on the screen and then open the program in 2 separate windows. I then compare the difference in the time displayed, and instead of the difference staying constant, it fluctuates quite substantially.
float counter = 0.0f;
void Update() { counter += Time.deltaTime * 1000; }
Unless I am understanding this incorrectly, no matter the framerate or when the players were opened, the difference between the value for counter should remain constant. Am I approaching this incorrectly, or is this a possible bug?
Answer by duck · May 13, 2010 at 07:17 AM
Yes, accumulating deltaTime into your own variable will always be less accurate than measuring Time.time directly.
Simply use Time.time instead of your counter variable and you should get much better results.
This won't work for my needs though, because I want a rolling counter to where it maxes out at 9999 ms and then starts back at 0. I simplified the code for the purpose of this. Is there no way to do that accurately?
Yes there is. Use: $$anonymous$$athf.Repeat( Time.time, 10000 );
did this solve your problem? (if so remember to vote up & accept the answer).
Sorry, I was moving out of my apartment for the past couple days and hadn't had a chance to test it out. Works like a charm though, thanks!
why is that? can you provide some sources for this claim? accumulating deltaTime should not be any more different than getting Time.time
Floating point error. Adding floats together isn't always accurate. For example, 2.0 + 2.0 = 3.999999 ins$$anonymous$$d of 4. Floats are weird.
Your answer
Follow this Question
Related Questions
Execute code every x seconds with Update() 4 Answers
Single Step (pause, resume), or externally clock game loop 0 Answers
Updating an array indices dynamically 2 Answers
Add 1 Per Second to an Int 1 Answer
Constant Yield Time 1 Answer