# 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?

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.

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

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);
}
``````

You’re basically trying to do what the documentation in the Scripting Reference tells you, correct? Unity - Scripting API: Vector3.Lerp

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);
}
}
``````