I was watching the Digital Foundry channel the other day. In it they highlighted an issue which comes up when you have a camera following a physics object in Unity.
at least somehow communicate this better to developers
— John Linneman
I’ll do my best, John. The problem is that, by default, the Unity physics system updates at a fixed rate of 50 times per second, whereas the displayed frames usually update at a rate of approximately 60 times per second. (The actual update rate might match your monitor settings, or just go as fast as it can.)
When the displayed rate and the physics rate are close but different, the number of physics updates between displayed frames will vary, and this will make physics objects in the scene visibly judder in play at a frequency equal to the difference. So 50 Hz physics and 60 Hz display will judder at 10 Hz, or once every 6th displayed frame. And if the camera is connected to that object, or is itself updated as a physics object, the whole view will judder at that frequency.
The following graph shows the apparent displacement of an object (in this case, the camera) displayed at 60 frames per second but updating its position at 50 frames per second in blue. In orange, you can see the equivalent displacement using interpolation.
Interpolation is Unity’s simplest solution to this problem. When rendering a physics object, it will instead pick a point between the two previous physics frames to compensate for the difference in update rates. This smooths out the apparent motion at the cost of lagging the actual position of the object by one physics frame.
To help show what’s going on, I uploaded a project to Github. This project contains two scenes. You should build and run these scenes rather than previewing them in the editor, as Unity editor playback has its own frame rate issues. And to get the same results you will need to view it on a monitor capable of running at 60 Hz and make sure your OS is set to display at 60 Hz in your settings. The problem is also visible with different frame rate combinations, but the frequency of the judder will be different.
Side By Side
The first scene shows a split screen with a ball rolling down a ramp. On the left, physics interpolation is off, and the issue described is visible. On the right, physics interpolation is on, and the issue goes away.
The graph at the bottom of each pane shows the acceleration of the camera object over time, where the camera is following the ball at a fixed distance and updating at approximately 60 frames per second.
In this scene, the ball has a Rigidbody
component attached. On the left, the Interpolate
parameter is set to None
, which is the default setting.
On the right, the parameter is set to Interpolate
, which fixes the issue.
It also saves out two CSV
files capturing the displacement of the camera at each frame and the time of the frame. One with interpolation on, the other with it off. These files will be save into your persistent data folder (%userprofile%\AppData\LocalLow\Powered Up Games\Follow Physics
on Windows). I used this data to create the graph above. And you can use these to see the effects of using different settings.
Press the TAB
key to switch to the next scene.
Toggle
In the next scene, you can toggle the interpolation setting on and off by pressing the SPACE BAR
and move the ball around with the W
A
S
D
keys. Because this movement is applied using the physics system, it exhibits the same issue.
The best way to see the difference is to give the ball a small forward force by tapping the W
key and then toggle with the SPACE BAR
. If you look carefully at the floor pattern as it moves past your view, you should notice the movement judder when interpolation is off.
Leave a Reply