- Home /
Vector3.Lerp and MoveTowards not smoothing (simple script)
public Transform target; public float smoothing = 5f; Vector3 offset; Vector3 targetCamPos;
void Start() {
offset = transform.position - target.position;
}
void FixedUpdate()
{
targetCamPos = target.position + offset; transform.position = Vector3.Lerp(transform.position, targetCamPos, smoothing);
}
I have copied this code from Unity tutorial. But if I put this code on my camera and attach the transform of my object(that I wanna follow) the camera fly's away in one direction that is based on the opposite position of the object. I have also tried to change update/fixedupdate, Time.deltaTime and so on. I never could see the smoothing change (when changing the value). Someone told me that it is a percentage but even then change the value from 0-1 had no impact (the camera would just follow or fly away) but no smoothing. Iam using MacBook Pro and I have the latest version of Unity. I can make a short video so u can see everything? Thanks in advance!
Answer by TreyH · Mar 05, 2019 at 01:37 PM
Differences between a position A and some other position B are usually given as (B-A), not the other way around (note that yours is A-B). (B-A) gives the vector required for A to reach B, so this is why your situation is flying away instead of going towards your target.
Since you can think of the t
value (your smoothing
) of a Lerp as how "close" the result will be to the two Vectors your provide, in your case it's returning a constant value. That offset
value will always be some stagnant vector, but your targetCamPos
is also being calculated as the current position plus that.
The code you posted would make more sense if you changed how your target position was being calculated:
targetCamPos = target.position + offset;
transform.position = Vector3.Lerp(transform.position, targetCamPos, smoothing);
This would preserve the original offset
between your object and its target. You can then change your smoothing
value to be something between 0 and 1, as you mentioned, with slow / fast values being close to 0 / 1, respectively.
I was getting the result I wanted when I put offset = transform.position - target.position + vectorDistanceCameraFromPlayer; in fixed update and assign the public Vector vectorDistanceCameraFromPlayer to lets say (1, 1, 1). Do u know Why?
Adding a constant to the offset shouldn't change anything here as your offset is itself constant and its value is just being used to keep a distance from something, so adding that (1,1,1) probably just prevented you from overshooting.
Answer by mike29willems · Mar 05, 2019 at 02:37 PM
Thankyou for ur time. I see that I made a mistake copying the last part. So I have now changed (B-A) and target.position + offset but the camera goes then trough the target in a straight line. If I change the Lerp to MoveTowards the camera goes to the target but is not keeping the offset distance and ones he reach the target he goes the same as lerp trough the ground and never turn back. Do you know how I can let the camera follow the target so that it travels always to the offset (with smoothing)?
Then it sounds like something is weird in your scene, is one of those objects the parent / child of the other? If I create an empty scene with:
using UnityEngine;
public class LerpFollow : $$anonymous$$onoBehaviour {
public Transform target;
private Vector3 offset;
void Start () {
offset = transform.position - target.position;
}
void FixedUpdate () {
transform.position = Vector3.Lerp (transform.position, target.position + offset, 0.1f);
}
}
Sitting on a cube with the target
assigned to another cube, I get:
With the black cube here being the target and the white cube maintaining its initial offset.
Even if you did it the other way around, with target.position - transform.position
as your offset, the white block would just be sitting on the other side.
Thank you! It finally works :) The mistake I made was that my camera previously flew away (bad coding) so when he did it again I thought that he was flying endlessly away (same as before), but when I checked the transform this time of the camera and saw that it was not changing anymore, I realized that it was because he had reached his offset. In my setup I couldn't tell by looking at the camera if he was still moving or not. So even if ur mistakes are looking the same while having other code check if its still true. Thank you for ur time and help!