Is that whole block of code inside Update?
If so, the issue is that you’re recalculating the endPos every time, so it’s always in front of the object.
If not, which code is in what method?
I think it would be helpful to see the complete script. There may be something off about the way this is being called.
But I think your problem is that you set the start and end points relative to player.gameObject.transform, while moving player.gameObject.transform. As the object moves, your start and end points change as well, which will lead to unpredictable behavior. You should store the start and end points before you begin the move and not change them in Update.
OK yeah, multiple issues with that code that weren’t visible in the original post. Mainly, you seem to be confused about how Lerp works.
Lerp doesn’t move anything. It doesn’t animate. It returns a value that’s C% between A and B, and that’s all it does. If you call it for one frame, it will move the object… one frame.
In order to use it to animate from point A to point B, you need to call it every frame until the movement is completed. There are a lot of ways to do that. Here’s my favorite for most situations:
StartCoroutine(LerpOverTime(startPos, endPos, duration);
IEnumerator LerpOverTime(Vector3 startPos, Vector3 endPos, float duration) {
for (float t=0f; t < duration; t += Time.deltaTime) {
player.transform.position = Vector3.Lerp(startPos, endPos, t / duration);
yield return 0;
}
player.transform.position = endPos;
}
You can insert this code (the first line in place of your line 38, the rest as a function in the class), and it will actually do what you thought Lerp did: it kicks off a coroutine, that will run code each frame, and run Lerp repeatedly during that time.
(You’ll want to make sure this is only being called once, in this case by setting startmove to false)