Hello everyone,
OK so I’ve been spending a couple of days on this, performing little experiments inside and outside Unity to try and figure out what has been going on. I won’t say I’ve got to the bottom of everything, but I discovered some stuff which I’ll share with you. If anyone else has some more findings I’m keen hear it too!
The first thing I have discovered is there is a big difference between Editor and Standalone Player on Windows. In the standalone player, the code calls timeBeginPeriod(1) whereas in the Editor it does not. For the difference, have a read of this:
Now if you think about it, this makes sense, because in the Editor you are typically multitasking, maybe you are at a Game Jam, you want your battery to last as long as possible. In the Standalone you are saying “I want the game to be as smooth as possible”. So that’s why animation is particularly uneven in the Editor.
In standalone, I didn’t get to the bottom of the occasional 1-frame glitching (although it does seem to be happening a lot less for me recently), but I think I can explain the variance in the deltaTime. I think it’s just inherent timing inaccuracy.
For this I took a standard DX9 program, with a Present call, and added this code to measure the time delta:
if (GetAsyncKeyState(VK_F1) & 0x8000)
{
LARGE_INTEGER qpft = {0};
QueryPerformanceCounter(&qpft);
long long now = qpft.QuadPart;
static long long prevTime = 0;
long long delta = now - prevTime;
double msec = (double)delta / 2435.778;
HWND hwnd = FrameWnd;
if (hwnd)
{
char text[128] = {0};
sprintf(text, "PresentFrame msec = %8.4f", msec);
SetWindowTextA(hwnd, text);
prevTime = now;
}
}
Sure enough the frame delta varied in just the same way: 16.88, 15.77…
So I think it’s just inherent to the PC/Windows system, not a Unity problem per se.
I’d be very curious if someone else can reproduce (or not reproduce!) my results with a non-Unity program.
I have heard that timing is a lot more consistent on PS4, which makes sense in an environment where there is a lot less going on in terms of other processes.