- Home /
Unsmooth vehicle-following camera
Hello,
I have written a smooth vehicle-following camera, but for some reason, it is shaky, like it was not updated often enough.
I've read several questions about this. Some people suggest to use LateUpdate() instead of Update(). I did this, but the problem remains. Some other people suggest to use FixedUpdate(). I tried that too, and it worked, but I'm afraid using FixedUpdate() is only hiding the problem without actually fixing it.
I saw in the scene view that the car itself was shaky, not the camera. Also when I drive, everything around me (objects, shadows, etc) are moving (relatively to the camera) smoothly. It's only the followed vehicle itself that is shaky (relatively to the camera only). I tried using a simple sphere instead of a complete car instead to make sure the complexity of the car model wasn't causing this, but I had the same problem. So I'm not sure about what is wrong.
Here is my code :
using UnityEngine;
public class RaceCameraController : MonoBehaviour
{
/// <summary>
/// Vehicle to look at.
/// </summary>
[Tooltip("Vehicle to look at.")]
public GameObject car;
/// <summary>
/// X rotation angle of the camera, relatively to the vehicle.
/// </summary>
[Tooltip("X rotation angle of the camera, relatively to the vehicle.")]
public float lookXAngle;
/// <summary>
/// Position the camera tries to reach progressively, relatively to the vehicle.
/// </summary>
[Tooltip("Position the camera tries to reach progressively, relatively to the vehicle.")]
public Vector3 carRelativeTargetPosition;
/// <summary>
/// Duration, in seconds, it would take the camera to reach its target position if the vehicle
/// immediately stopped.
/// </summary>
[Tooltip("Duration, in seconds, it would take the camera to reach its target position if the vehicle immediately stopped.")]
public float repositioningDuration;
void LateUpdate()
{
Vector3 targetPosition = car.transform.TransformPoint(carRelativeTargetPosition);
Quaternion targetRotation = car.transform.rotation * Quaternion.Euler(lookXAngle, 0f, 0f);
float progress = Mathf.Min(Time.deltaTime / repositioningDuration, 1.0f);
transform.position = Vector3.Lerp(gameObject.transform.position, targetPosition, progress);
transform.rotation = Quaternion.SlerpUnclamped(transform.rotation, targetRotation, progress);
}
}
Do you have any idea about what could be wrong ? Thanks.
two things you could try. On the car's rigidbody. Try changing the interpolate setting. also try to use a character controller if you allready have not.
Answer by Namey5 · May 30, 2017 at 06:13 AM
FixedUpdate isn't hiding the problem, it is the actual (documented) fix. Physics in Unity runs in a different time frame to the normal Update call. Unity's alternative to make adjustments line up with physics calculations is to use FixedUpdate, that is its purpose.
https://docs.unity3d.com/ScriptReference/MonoBehaviour.FixedUpdate.html
Thanks for your help ! I used LateUpdate in the first place because the documentation says that :
For example a follow camera should always be implemented in LateUpdate because it tracks objects that might have moved inside Update.
(here : https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.LateUpdate.html)
So for what reason exactly is FixedUpdate the right solution ? Thanks.
If you want more detail about it, basically it's down to the way the physics engine is updated. In Unity, to save resources this isn't done every Update (every frame), ins$$anonymous$$d it's done every Fixed Update (hence the name). The Fixed Update is controllable for accuracy reasons, meaning you can change how frequently it is called, and therefore how smooth physics objects are updated. It also means that a drop in frame rate will not alter the movement of a physics object, as the time between each Fixed Update is always the same, but the time between each Update may change.
There is a fairly simple explanation in the video in this tutorial.
https://unity3d.com/learn/tutorials/topics/scripting/update-and-fixedupdate
Thanks, but I still don't understand the reason of this unsmoothness. FixedUpdate is likely to be called more often than Update, fine, but this doesn't tell me why the camera looks so unsmooth. I'm running the game with 80 FPS, that's more than my screen can display, so I should not be able to see the steps of the camera movements. Is there anything wrong with my calculations ?
Your answer
Follow this Question
Related Questions
custom follow script choppy movement 2 Answers
camera follow an game object 0 Answers
Camera Border Follow 1 Answer
Smooth Follow Problem 1 Answer