- Home /
Lerp - Time Remaining
Here's my problem,
I'm moving a character with Vector3.Lerp() and when he's 1.2 seconds away from reaching his destination I want to play an animation.
So I'm trying to figure out how to calculate this and it's got me stumped.
This is what I got so far:
// time passed since character began moving
float timePassed = Time.time - startTime;
// distance traveled since the character began moving
float distance = transform.position.z - startPos.z;
Now say I Do
float speed = distance - timePassed;
that will give me units/sec ?
Then to get the distance between the current position and the destination
float remainingDistance = destination.z - transform.position.z;
Now I'm stuck, I feel like I have all the pieces I'm just not sure how to put them together.
Can you post the Lerp() code? The answer will depend on how you are using Lerp().
Answer by robertbu · Dec 07, 2013 at 02:49 AM
float remainingSeconds = remainingDistance / speed;
This will give you the remaining time assuming that the movement is linear. But there are other uses of Lerp() that produce eased movement that is not linear. For example if you use this form of Lerp():
transform.position = Mathf.Lerp(transform.position, dest, speed * Time.deltaTime);
Then you cannot predict when 1.5 seconds...at least not easily.
Ok, so lerping like in this example
http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Lerp.html
will give the desired results?
Since you already marked the question as answered, I'm sure you figured it out, but the use of lerp in that example will work with the above calculation. As long as the movement is linear ins$$anonymous$$d of eased, it will work.
Your answer
Follow this Question
Related Questions
Lerp time and movement not in sync 1 Answer
how to slow down speed increase? 2 Answers
Moving multiple objects at same speed 1 Answer
set the speed in respect of time 2 Answers