# Forward and backwards motion problem.

Hello everyone,

I’m really having problems trying to accomplish a very simple thing in Unity. I’m trying to make an object move forward until it reaches certain point, and then, after a certain time has passed, move away from that point to its initial position. This is the code I’m trying to use:

``````function Update () {
transform.Rotate(0,0,rotate_speed);

UpAndDown();

if(direction == 1){transform.position = Vector3.Lerp(up_transform.position, down_transform.position, Time.time);}
else if(direction == -1){transform.position = Vector3.Lerp(down_transform.position, up_transform.position, Time.time);}
Debug.Log(timer);
}

function UpAndDown(){

timer += Time.deltaTime;

if(direction == 1 && timer  >= 5.0){
direction = -1;
timer = 0;
}

if(direction == -1 && timer >= 10.0){
direction = 1;
timer = 0;
}
``````

This code though, it just works for the first time, the object goes down, and then it just teleports between the two locations, it doesn’t make a movement motion.
What am I doing wrong?

PD: I couldn’t post the variable declaration as a code, but the up_transform and down_transform are the two locations that I want the object to go between, the object is also rotating but this works fine, also the timer is a float variable and the direction variable is just an int variable.

Thank you

The problem is that your last Lerp arg is Time.time.
It needs to be a value from 0 to 1, like:

``````timer/10
``````

(5 for direction 1)

For this to work, Time.time should vary from 0 to 1 each time you call Lerp, what obviously will never happen. The best way to move with Lerp is using a coroutine to vary the t parameter from 0 to 1 at the adequated speed. You must not call the coroutine again until it finishes the movement - so I added a flag to avoid this. And if you want this movement to repeat forever, use another coroutine called in Start (because it should be called only once).

Why are you rotating the objec at Update? Should it rotate once before each cycle (case 1)? Or should it rotate continuously while moving (case 2)? I included both cases in the code: just uncomment the corresponding line:

```function Start(){
UpAndDown();
}

function UpAndDown(){
while (true){ // repeats forever
//  transform.Rotate(0,0,rotate_speed); // <- case 1
MoveObj(transform, up_transform.position, down_transform.position, 3); // start up-down movement
yield WaitForSeconds(5); // wait 5 seconds
MoveObj(transform, down_transform.position, up_transform.position, 3); // start down-up
yield WaitForSeconds(5); // wait other 5 seconds
}
}

var moving = false;

function MoveObj(obj:Transform, org:Vector3, dst:Vector3, time:float){
if (!moving){ // don't start another cycle until while other is running
moving = true; // cycle started
for (var t:float=0; t<1;){
//    transform.Rotate(0,0,rotate_speed*Time.deltaTime); // <- case 2
t += Time.deltaTime / time; // increase t at the right pace
obj.position = Vector3.Lerp(org, dst, t); // move object a little each frame
yield; // return here next frame
}
moving = false; // cycle ended
}
}
```