- Home /
ricreate Lerp function
I'm trying to realize the function of Lerp Vector3 class. This is what I did. Do you have any idea on why it does not work?
pragma strict
var start :Transform;
var end :Transform;
function Start () {
}
function Update () {
tra(start.position,end.position,12.0);
}
function tra(from:Vector3, to:Vector3, t:float){
t = Mathf.Clamp01(t);
return new Vector3(from.x + (to.x - from.x) * t, from.y + (to.y - from.y) * t, from.z + (to.z - from.z) * t);
}
Answer by Eric5h5 · Aug 07, 2014 at 04:58 PM
The function itself would work, but you're not doing anything with the return value, plus you're passing in 12.0 when it's clamped from 0.0 to 1.0. Also, the Vector3.Lerp function already exists in Unity.
Answer by Andres Barrera · Aug 07, 2014 at 05:07 PM
Hi,
If you want it to work with the Update, you should change that last value every cycle. Something like this:
float lerpSpeed;
float lerpPercent = 0;
Update()
{
tra(start.position, end.position, lerpPercent);
lerpPercent += lerpSpeed * Time.deltaTime;
}
Edit: see the previous answer, you also need to assign the return value to the transform.position you want to move.
Your answer
![](https://koobas.hobune.stream/wayback/20220613160054im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Vector3.lerp doesn't work 1 Answer
Stop a Lerp from looping 3 Answers
Move an object through a set of positions 2 Answers
How to update interval between objects in run time? 0 Answers