There are two problems in the code:
Vector3.Lerp returns a single value. Since your function will be called only once, you simply set the position to all Lerp . Instead, you want to reposition each frame. Use coroutines for this.
Time.DeltaTime returns the time elapsed since the last frame, which will usually be very small. You want to go in the range from 0.0 to 1.0 depending on the course of movement.
Then your code will look like this:
IEnumerator MoveFunction() { float timeSinceStarted = 0f; while (true) { timeSinceStarted += Time.DeltaTime; obj.transform.position = Vector3.Lerp(obj.transform.position, newPosition, timeSinceStarted); // If the object has arrived, stop the coroutine if (obj.transform.position == newPosition) { yield break; } // Otherwise, continue next frame yield return null; } }
source share