- Home /
Camera update when tracking physics controlled object
We're developing a side scrolling platform endless 'runner' game for mobile.
The character is a rigidbody with colliders and the environment has colliders which the character travels along at a constant rate in the +x axis. To move the character we use Rigidbody.AddForce with ForceMode.VelocityChange (+x axis for running, +y axis for jumping as needed).
This works well for us as the character behaves as we want and we also get all the collision/trigger events we need.
The issue I have is with the camera - it's ok, but could be smoother (the perceived 'scrolling' of the environment could be smoother).
The camera tracks the character but of course the camera frame rate is variable and not the same as the physics update which moves the character. What this means is that each render frame the distance the character has travelled since last time can vary - which leads to the enviroment 'scrolling' in a unsmooth way.
This shows up really bad in the editor on my iMac because the update time is pretty varied each frame. It's much better on my iPad because of the mostly consistent update time (60fps) but could be better.
Can anyone offer some advice?
You mean for camera positioning? Yes, I already do that. I believe that the issue is because the number of physics steps can be different between renders.
One thing that you can do, is to create an empty gameobject that will follow your character using some smoothing algorithm, and you use this gameobject as reference point for your camera tracking. It can even look better than to keep your character exactly on the center of your screen all time and that hiccups of updating the position and rotation of the camera to the new values.
(dumb smooth sample)
targetPosition = newPosition + oldPosition - (oldPosition - newPosition) * 0.1f;