- Home /
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;
you're lerping the whole position for the time the while loop runs. this means z does not change during that time and keeps the player in place. only lerp x and use a fresh transform.position each time you need to apply it.
I think i understand, But would not that weigh in CPU processing? it will be need to Calculate Frame per Frame wher should go, or did I misunderstand?
Answer by Shadownildo · Dec 16, 2018 at 02:57 PM
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;
Your answer
Follow this Question
Related Questions
Projectile isn't reaching my target. 1 Answer
Is there a way to lock velocity? 3 Answers
Imparting Physics in only certain planes of motion 1 Answer
Player slows down when jumping/velocity changes 1 Answer
Reflect bullets in 3D world space 1 Answer