Why does this work?
Hi, this may sound like a slightly weird question, but recently I've implemented a script written by another user on the forum for making an object follow the mouse, and it seems to work absolutely fine - my only problem being I'm not so sure I understand the logic behind it. Here's the code:
Vector2 mousepos = Input.mousePosition;
mousepos = Camera.main.ScreenToWorldPoint (mousepos);
transform.position = Vector2.Lerp (transform.position, mousepos, speed);
Now I'm fairly sure that Vector2.Lerp uses the third parameter as an interpolant between the values 0 and 1, in which an example value of 0.5 would make the object move halfway of the distance between its current position and the target position. However, for the object to eventually move to the target position, wouldn't this parameter need to be incremented until it eventually reaches 1? Yet in this code the variable speed never changes (it is currently set at 0.05) yet the object still perfectly follows the mouse. I've obviously misunderstood the Lerp function, so if someone could explain it would be very much appreciated.