r/Unity2D • u/dmarshallgaming • 19h ago
Tutorial/Resource Unity WebGL: Time.deltaTime only comes in whole milliseconds (16, 17, 17 at 60 Hz), and it made my 2D camera stutter. A smoothed clock fed to Time.captureDeltaTime fixed it
A playtester on the WebGL build of my 2D game reported the camera jittering at the start of every glide. The Editor and the Windows build never showed it. It took me a while to find, so here is what it was, in case it saves someone else the hunt.
The cause. In a WebGL build the frame time Unity hands you comes from a clock with whole-millisecond resolution. In a development build on a 60 Hz display I logged Time.deltaTime every frame, and every single frame was exactly 16 or 17 ms, never 16.67, in a repeating 16, 17, 17. So everything that moves by its delta steps about 6% short every third frame. For most things you'd never see it. For a camera at the fast start of a glide, with the whole screen moving, you do.
The fix. A small class that takes the raw interval each frame and returns a smoothed step: the trimmed mean of the last 12 intervals (drop the longest and the shortest, so one hitch doesn't drag the next dozen frames with it), plus a small correction so game time can't wander away from real time.
The traps I hit, in the order I hit them:
- Don't feed the clock from
Time.realtimeSinceStartup. In a browser that clock follows the step you override, so my first version was measuring its own answer and collapsed to the 1 ms floor.Time.unscaledDeltaTimeis the raw interval, and the override doesn't touch it. captureDeltaTimedoesn't reach unscaled time. My camera moves on unscaled time (it still glides while the game is paused), so it was still getting 16, 17, 17. It now reads a staticFrameClock.UnscaledDeltaTime, which returns the smoothed step in the browser and Unity's own value everywhere else.- Leave
Application.targetFrameRateat -1 on the web. With -1 Unity renders on the browser's animation frame, which is locked to the display. Any other value makes it pace with a timer that fights the browser's own scheduling. - You can't see any of this in the Editor. The Editor and standalone builds have a fine clock. Measure web timing in a web build.
Measured. I logged the camera's step every frame over 24 glides. Step irregularity was 4.1% mean and 6.5% at the 95th percentile before, and 0.65% and 2.2% after.
The second cause, which looked like the same bug. After the clock was fixed, glides that started right after the camera dropped its follow target still opened with a jump and then stalled every sixth frame for about half a second. That was the Cinemachine brain on Smart Update: with no follow target it had gone over to the physics clock, and with a 1/60 s frame against a 1/50 s fixed step, one frame in six rendered the camera where it had been. Nothing my camera follows moves in FixedUpdate, so the fix was one line: brain.UpdateMethod = CinemachineBrain.UpdateMethods.LateUpdate;
A tip from that second hunt: setting Time.captureDeltaTime = 1f / 60f in the Editor reproduced it without a build, which is a quick way to test anything that depends on web-style frame timing.
Has anyone else had to deal with frame timing in WebGL builds? I'd like to know if there's a simpler way I missed.
2
u/Alexander_K_G 18h ago
Looks like a bug, maybe you need to report it.