# Using Lerp in a Constant Moving Object

Hi there Folks, I really need a little help in that part of the code, I’m with an object that is constantly moving in position Z forward, and I need to move it in position X without it losing speed. But what happens is exactly that, when he uses Lerp, it loses speed for a few thousandths of seconds and ends up creating a strange and really annoying effect…

private IEnumerator Move(int whereToMove)// 0 Up ; 1 Down- ; 2 Left ; 3 Right
{
switch (whereToMove)
{
case 0:

``````            break;

case 1:

break;
case 2:
isMoving = true;
runTime = 0;
startSlimePosition = transform.position;
endSlimePosition = new Vector3
(startSlimePosition.x - 2.80f, transform.position.y, transform.position.z);

while (runTime < runDuration)
{
runTime += Time.deltaTime;
transform.position = Vector3.Lerp
(startSlimePosition, endSlimePosition, runTime / runDuration);
yield return null;
}

isMoving = false;
break;
``````

Already Solved, i modify a little the code, I saved the position in a variable outside the while, and in While I set a new Vector3 within Lerp and now it is working perfectly

private IEnumerator Move(int whereToMove)// 0 Up ; 1 Down- ; 2 Left ; 3 Right
{
switch (whereToMove)
{
case 0:

``````            break;

case 1:

break;
case 2:
isMoving = true;
runTime = 0;
startSlimePosition = transform.position;
// endSlimePosition =
endpositionX = startSlimePosition.x - 2.80f;
while (runTime < runDuration)
{
runTime += Time.deltaTime;
transform.position = Vector3.Lerp
(startSlimePosition, new Vector3
(endpositionX, transform.position.y, transform.position.z), runTime / runDuration);
yield return null;
}

isMoving = false;
break;
``````