The thing is, in this case, Lerp moves it to a point in between transform.position and destination, right? So that if it is at 0, it stays where it is, if it’s at 0.5, it moves half way between the two, etc. Time.deltaTime will be something like 0.016 (at 60 fps), so it will move a little bit towards the destination, slower at first but faster and faster later on. So far so good, right?
The problem: it will never really reach the destination. It only “reaches” it when the floating point error is so small that the two values are technically speaking equal, but floating point errors are handled differently on each computer.
As a freelancer, I once took over a project to finish it and it had the most interesting bug. There was a feature where you press a button to zoom out, and it would make the zoom buttons disappear, transition the camera to a zoomed out position, then make the buttons reappear. It would work fine on my computers (all 4 of them, with Winodws 7). It worked fine on my employers’ macs. But on this one guy’s Windows PC, when you pressed the zoom button, it would move the camera to the proper location, but then the buttons would never reappear and the game appears frozen.
I spent hours digging through the previous freelancer’s code (it was a large project, and the code was, frankly, horribly written), until I came upon a gem just like that, using Lerp in the same way to move the camera. And it would have a check like
if (transform.position == destination)
//show buttons again and continue game.
Turns out, I have an Intel processor on all my computers. All macs used Intel processors as well. That guy with the bug had an AMD processor, and apparently they handle floating point errors differently.
So I changed that to something like
transform.position = Vector3.Lerp(initialPosition,destination,currentTime/totalTime);
currentTime+= Time.deltaTime;
And it worked perfectly on all computers. It would ALWAYS reach the destination when currentTime>=totalTime. As far as I am concerned, Lerp should always be used with a value that reaches 1 at some point.
Want some fancy smooth effect? Add a public AnimationCurve curve; to the script and then do this:
Vector3.Lerp(initialPosition,destination,curve.Evaluate(currentTime/totalTime));
This way you can set the easing curve just how you want it in the editor, have better control over everything, and make sure it works on all computers.