- Home /
How can I use Lerp to move an object from its current location to another in exactly 1 second?
I really need help with code to do this, I've used parts of examples I've found on the internet but I don't understand how they work. I'm more interested in whats happening, not just getting things working. Could someone present me with this code and really give a good explanation of how it's working out to be exactly 1 second from point A to point B?
Answer by Wolfram · Jan 11, 2013 at 12:49 PM
The best and most powerful approach would be to use iTween.
However, if you really want to use Lerp, use something like this (C#):
public float delayTime;
public Vector3 posA;
public Vector3 posB;
void Start(){
StartCoroutine(WaitAndMove(delayTime));
}
IENumerator WaitAndMove(float delayTime){
yield return new WaitForSeconds(delayTime); // start at time X
float startTime=Time.time; // Time.time contains current frame time, so remember starting point
while(Time.time-startTime<=1){ // until one second passed
transform.position=Vector3.Lerp(posA,posB,Time.time-startTime); // lerp from A to B in one second
yield return 1; // wait for next frame
}
}
The advantages of using a Coroutine instead of putting this in Update is you are free to choose when to launch the interpolation, you don't keep messing with object's position during the whole game, and you gain performace since you do the computations exlusively during the 1 second of interpolation, not continuously during the whole game.
Do not use the Lerp(A,B,Time.deltaTime) approach you often see. The effect of that will rather be a smooth/dampened translation of unspecified length.
Should ideally use yield return null, since that performs no memory allocations whereas yield return 1 does.
Ah thanks, didn't find a documentation about that return value anywhere.
Answer by ganesh.pingale · Jan 11, 2013 at 12:47 PM
Use Following Script apply it on object that you want to move and use Empty Game Object as target.
// Animates the position to move from start to end within one second
var start : Transform;
var end : Transform;
function Update () {
transform.position = Vector3.Lerp(start.position, end.position, Time.time);
}
The major disadvantage with this example from the docs is, the Lerp always starts as soon as pressing play, and ends one second after that, and it is unclear for the layman how to change that, or why there is the absolute game time in a Lerp parameter that's supposed to be valid only from 0-1. Or why it is impossible to change that object's position ever again in another script, since this Update() keeps overriding the object's position although the Lerp might have "finished" ages ago.
Also, for example, something as simple as "do the interpolation in 2 seconds ins$$anonymous$$d of 1" is not obvious, and somebody new to program$$anonymous$$g might not see it at all without additional help.
I am new to unity also I don't know such thing. and thanks for the reply!!
Don't worry, it was rather a complaint about this particular doc page, not a rant about you posting it ;-)
Answer by MarigoldFleur · Jan 11, 2013 at 12:47 PM
You're basically trying to do what the documentation in the Scripting Reference tells you, correct? http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Lerp.html
Javascript
// Animates the position to move from start to end within one second
var start : Transform;
var end : Transform;
function Update () {
transform.position = Vector3.Lerp(start.position, end.position, Time.time);
}
C#
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public Transform start;
public Transform end;
void Update() {
transform.position = Vector3.Lerp(start.position, end.position, Time.time);
}
}
Dang, should have looked at the docs before answering ^^
But also see my comment in ganesh's answer.
Answer by Watapax · Sep 16, 2013 at 03:58 PM
I had to do the same, and what occurred to me was simply to create an animation with the animation editor in unity, then just call the animation you just created from the script