- Home /
Vector3.Lerp is not reacting, pls help (simple script)
public Transform target; public float speed; public Vector3 offset;
private void FixedUpdate()
{
Vector3 DesiredPosition = target.position + offset;
transform.position = DesiredPosition;
Vector3 Smoothing = Vector3.Lerp(transform.position, DesiredPosition, speed);
transform.position = Smoothing;
}
Changing the smoothing(speed) doesn't effect the smoothing(speed) let a lone getting a smoothing. I made a simple cube with a movement script and a camera with a camera script^^. Only when I would change the cube script to update and camerascript to fixed update I would get some smoothing but then again changing the smoothing(value) didn't affect it and it glitched. I have a MacBook Pro and iam running the latest version of Unity. The weird thing is that I literally copied the scripts from Youtube_Unity and Youtube_Brackeys but they didn't work I tried a lot am I missing something? (By the way there was even a point where my camera flew the opposite way in a straight line of what it should follow... )
Lerp doesn't work how you think it does. The 3rd parameter of lerp is not a "speed" parameter, its the distance from point A to point B in percentage. So if you were to lerp between A and B at .5f then you would end up always being at the center between both objects. Lerp is simply a linear interpolation between 2 points. What you want is like transform.position = Vector3.$$anonymous$$oveTowards(transform.position, desiredPosition, speed * Time.fixedDeltaTime);
Answer by WarmedxMints · Mar 05, 2019 at 12:29 AM
You have a few mistakes there Firstly,
unless your transform has a rigidbody attached, you should really be updating its position in Update as FixedUpdate is for physics instructions.
You are setting the transform's position to your desired position before you even start the lerp.
It looks like you are confused over the 3rd parameter for the lerp method.
The 3rd parameter of the lerp method which Unity calls time could also be called percent. It is not how long the lerp will take, it is a percentage from the startpoint to the endpoint. The method uses a range of 0 - 1 for this. So 0 would return the start point (1st parameter) and 1 would return the endpoint (2nd parameter). The method also clamps the time parameter to 1 so even if you put 1000 in there, it would still return the endpoint.
This code should do what you wish;
private void Update()
{
var DesiredPosition = target.position + offset;
transform.position = Vector3.Lerp(transform.position, DesiredPosition, Time.deltaTime * speed);
}
Although I would recommend using MoveTowards instead
private void Update()
{
var DesiredPosition = target.position + offset;
transform.position = Vector3.MoveTowards(transform.position, DesiredPosition, Time.deltaTime * speed);
}
Answer by mike29willems · Mar 05, 2019 at 11:59 AM
Thank you for ur reply, but it didn't worked... I copied ur script (and MoveTowards) but the camera just followed the target without smoothing. Since I am working on a project this is really annoying. This script is from Unity, is this also rong? (target is moving in fixed update and has a Rigidbody). Lastly what value do I need to paste in smoothing to be sure it really should smooth? Do I need to make a quick video cus it is still not working so u can see everything.
public Transform target; public float smoothing = 5f; Vector3 offset;
Void Start () { offset = transform.position - target.position;}
Void FixedUpdate() { targetCamPos = transform.position + offset; transform.position = Vector3.Lerp (transform.position, targetCamPos, smoothing); }
Your answer
Follow this Question
Related Questions
Smooth animation scrubbing with the mouse 3 Answers
How can I get a game object to follow something smoothly without using lerp. 0 Answers
why target switching of smooth camera not very smooth ? 0 Answers
Vector3.Lerp and MoveTowards not smoothing (simple script) 2 Answers
Moving player in an arc, from startPoint to endPoint 2 Answers