I am recieving data every server tick (25 ticks per second) and I need to count ticks per second on client. I am using Time.time for measuring those ticks (I tested even Time.fixedTime) and when I have Vsync turned on I see 26t/s, when half vsync I see 27t/s.
I know that Time.time ansd Time.fixedTime is counted by number of screen refreshes, but I need to count ticks independtly on screen refresh rate? Is there something like microtime() in PHP? I need time what is independent on screen to properly calculate ticks per second.
Unity - Scripting API: Time All the Unity time. Maybe one of them is what you are looking for.
Don’t use the Unity time if you need to measure time independent of the unity runtime. All of its time is tied to the game/refresh/physics/etc.
Instead use some sort of system time.
The .net/mono framework (which is the framework you get access to writing C#) is full of various different ways to measure system time. The one you choose really hinges on the sort of accuracy you need.
There’s the ever obvious System.DateTime.Now. The resolution of this is not perfect (it can be off by a couple milliseconds), but is what is relatively the same as PHP’s ‘microtime’, since ‘microtime’ really just calls the system ‘gettimeofday()’, which is what System.DateTime.Now does.
Where as something like say System.Diagnostics.Stopwatch measures change/elapsed time pretty fairly accurately, in the nanoseconds. But doesn’t operate on getting the current time of day, but instead the amount of ticks that passed since ‘Start’ was called on the Stopwatch (a Tick is 1/10millionth of a second). Inaccuracy comes from the overhead of calling Start/Stop/Elapsed.
Use something like this if you need to measure the difference in time independent of Unity.
…
Note, System.DateTime also uses ticks, it’s just that the retrieval of system time isn’t necessarily accurate. This also means that microtime isn’t super accurate in php neither.
In the end though, trying to gather system time is never going to be 100% accurate, never trust that. You want something near the accuracy you require. With that said, Stopwatch is pretty damn accurate for most syncing needs… and depending the system you run it on, it tries to use the most accurate counter it can (read the documentation and it gets into specifics about that with the IsHighResolution property).
All are dependent on frame rate or physics
@lordofduct
This is exactly what I wanted. Thanks for explanation.
But there is LITTLE problem System.DateTime or Stopwatch still giving me same values as before. Still depends on vSync
Could it be that you mean you’re reading it at a certain frame and that gives you very similar results, but in the 1 thread, there isn’t another way to read the time…
To be honest, I don’t fully understand your question.
Unless I am just confused… that happens…
DateTime and Stopwatch do not depend on vSync in any way shape or form.
You must be reading them at times relative to vSync. Such as in Update.
1 Like