# Lerp an object based on timers

I'm trying to make a target lerp between two objects based on a timer.

At the moment, I have the following code:

   float distCovered = (Time.time - waitTime) * speed;
float fracJourney = distCovered / journeyLength;
if (_moveDown == false)
{
if (startTime + waitTime < Time.time)
{

transform.position = Vector3.Lerp(start.position, end.position, fracJourney);

if (transform.position == end.position)
{
Debug.Log("going down");
_moveDown = true;

transform.position = Vector3.Lerp(end.position, start.position, fracJourney);
}

}
}

if (_moveDown == true)
{
float distCovered1 = (Time.time - goDowntimer) * speed;
float fracJourney1 = distCovered1 / journeyLength;
transform.position = Vector3.Lerp(end.position, start.position, fracJourney1);

if (transform.position == start.position)
{
Debug.Log("going up");
// waitTime = 20;
_moveDown = false;

}

}


This code is in my update function and is attached to each of my objects that I want to move up and down. Each object is able to set their wait time independently of the others, thus i can have 1 move after 5 seconds, another after 10 etc.

Then, each target waits a few seconds and moves back down. However, the movement isn't smooth and it tends to jump a set distance. But then, when it gets back to the bottom it goes crazy between the _movedown bool and wont move.

Does anyone know of a way I can fix these issues?

I do know of the Mathf.PingPong method that constantly moves the object back forth between two points, but that wont allow me to pause the movement at each section. Though, if someone knows a way I can do this, please let me know as well.

• This wasnt part of your question but i noticed your distCovered and was wondering if you were intending to use time as distance or did you want to get the actual distance in 3d space that the object has moved? – Mungoid Mar 28 '13 at 16:14
• The whole lerp line, and the arguments it takes comes from the Unity doc example. – N0xus Mar 28 '13 at 16:17

Im assuming this is in unity so I'll suggest based on that. What you should use is the deltaTime to increment your lerp. You can find that in Time.deltaTime and just use that for the third parameter in Lerp. Or if you need to speed it up or slow down you can multiply that delta time by a number you deem the speed.

For the waiting (it looks like you were trying something with waitTime), you just need to decrement the waitTime at the end of your update by deltaTime and do an if(waitTime <=0) to determine if you can continue to move on.

I didnt really have time to whip up some code for you but if my explanation didnt make sense I can try and get something put together for you =-)

Edit: I decided to quickly modify your code. I havent tested this but i think it should work

void Update () {
float distCovered = (Time.time - waitTime) * speed;
float fracJourney = distCovered / journeyLength;

if (_moveDown == false)
{
if (waitTime <= 0)
{

transform.position = Vector3.Lerp(start.position, end.position, Time.deltaTime);

if (transform.position == end.position)
{
Debug.Log("going down");
_moveDown = true;

transform.position = Vector3.Lerp(end.position, start.position, fracJourney);
}

}
}

if (_moveDown == true)
{
float distCovered1 = (Time.time - goDowntimer) * speed;
float fracJourney1 = distCovered1 / journeyLength;
transform.position = Vector3.Lerp(end.position, start.position, Time.deltaTime);

if (transform.position == start.position)
{
Debug.Log("going up");
waitTime = 20;
_moveDown = false;

}

}

if(waitTime > 0)
waitTime -= Time.deltaTime;

}