Peeta
August 7, 2014, 4:41pm
1
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);
}
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.
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.