- Home /
Lerp has some bumping effect
Hello,
I took this script from the unify wiki, that it is supposed to smooth the movement of the camera. I wanted to follow a race car, but when he car moves, the camera is a bit bumpy. How can I fix that?
var target : Transform;
var distance = 3.0;
var height = 3.0;
var damping = 5.0;
var smoothRotation = true;
var rotationDamping = 10.0;
function Update () {
var wantedPosition = target.TransformPoint(0, height, -distance);
transform.position = Vector3.Lerp (transform.position, wantedPosition, Time.deltaTime * damping);
if (smoothRotation)
{
var wantedRotation = Quaternion.LookRotation(target.position - transform.position, target.up);
transform.rotation = Quaternion.Slerp (transform.rotation, wantedRotation, Time.deltaTime * rotationDamping);
}
else transform.LookAt (target, target.up);
}
Answer by Dzoni94 · Aug 07, 2014 at 12:08 PM
Vector3.Lerp and Quaternion.Slerp interpolate between two values based on the value being the last parameter. Setting that value to 1 will be the same as not using Lerp/Slerp at all. If the camera is "bumpy" in your case, the issue might be the value set is too close to 1, which makes the camera get to target position/rotation too fast. Try reducing the damping and rotationDamping based on "bumpiness" in position/rotation of the camera.
Also, what might be the issue. The car is probably moved by a code in another script which is also executed in Update or FixedUpdate function. Positioning the camera should be done AFTER all the car positioning has been done, so you should change the Update to LateUpdate in this script. That will make sure the camera is trying to chase the new, correct, position of the car, rather than chase the old/current/new position, based on the execution order of the scripts.
It stops being bumpy from values < 1.0 but then, the camera gets too far from the car
If I change to LateUpdate, it is even more bumpy! This is the script I use to move the car
#pragma strict
var speed : float;
var maxSpeed : float;
var turnSpeed : float;
function Start () {
}
function FixedUpdate () {
var v = transform.forward;
v.y = 0.0;
v.Normalize();
v *= Input.GetAxis("Vertical") * speed * Time.deltaTime;;
v.y = rigidbody.velocity.y;
rigidbody.velocity = v;
transform.Rotate(0,turnSpeed * Input.GetAxis("Horizontal"),0);
}
You are directly setting the velocity of the rigidbody.
Since FixedUpdate runs at a fixed time interval the change in velocity is at a constant rate.
As always stated, perform physics inside FixedUpdate.
FixedUpdate gets executed in fixed time intervals. Setting both camera and car function to FixedUpdate makes them execute the same number of times per second which gets them synced. Although you shouldn't do anything not physics related in FixedUpdate ( it doesn't mean it's impossible, just not advised ), and it's a good thing to only move the camera in LateUpdate after all other calculations are done so it renders stuff corectly.
Your answer
Follow this Question
Related Questions
How you change another script value smoothly on trigger 0 Answers
moving between two points over time 1 Answer
How can I smooth out the crouch movement? 1 Answer
Smooth Mouse Orbit c# 2 Answers
Smooth Camera/Object Movement 1 Answer