- Home /
Smaller Time Interval?
I know Time.time keeps track of seconds in Unity, but is there a smaller timer available similar to System.currentTimeMillis() or System.nanoTime() in Java?
In general, the question is just for inquiry. But I suppose in practice it could be used to keep time scales in sync across all devices with different cpu clock speeds. That's the general application with the two mentioned above functions in Java since it too is cross platform. Or does Unity do this automatically?
Answer by Fattie · Oct 14, 2012 at 08:40 AM
To some extent I think the answer is "yeah it's automatic in Unity"
i encourage you to read this general answer about high speed real time networking in Unity (which is very very hard, as on any platform)
http://answers.unity3d.com/questions/329457/few-questions-regarding-multiplayer.html
U should also look in to realtimesincestartup, invokerepeating, and many questions on here about such time precision issues!
i never worry about anything less than the odd tenth of a second so don't ask me :)
TBC, "different cpu clock speeds" yes you utterly do not need to consider that. Unity absolutely, definitely takes care of that level for you :)
Could it be what you are getting at is the difference between Update() and FixedUpdate()
So, you have a car that moves. if you mistakenly use Update() incorrectly re the physics, etc, the car will indeed move at different speeds on different platforms. But FixedUpdate() will run at the same speed on different platforms, so the game will work correctly for everyone on the leaderboard!
Answer by Bunny83 · Oct 14, 2012 at 08:48 AM
Time.time is a float so it's numeric precision can vary depending on the length of the number but it's usually way better than your actual timestep you have between frames.
Unity, or most games in general, are frame-based. Time.time will not change during one frame. It only changes from frame to frame. A frame represent logically one infinite small step. This prevents inconsistency within the same frame. If you want to get a timedifference within one frame you can use Time.realtimeSinceStartup which does work frame independent.
You can also use the usual timers which comes with the .NET / Mono Framework. Keep in mind that not everything that is possible in .NET will work on all platforms.
Frame updates are great for rendering in games, but not so much for communications and logic, as Fattie pointed out, which need to stay constant across platforms. This was exactly my point too and his answer is closer to what I was looking for. Thank you anyway for the information! I honestly didn't even know C# had an HR timer.
Bunny is drunk most weekends so don't worry too much about anything he says :-) (Between us ... schnapps.)
lol seriously I mainly just repeat info that B has told me over the years! :)
Your answer
Follow this Question
Related Questions
How Would I Change This To Read Within A Time Range? 1 Answer
How can I have timespan display less 2 Answers
How to impliment a 10 minute day clock, and tie it to a directional light rotation. 4 Answers
Digital clock 1 Answer
Unity3d School 1 Answer