Hi, I’m currently messing with the lerp function but I don’t really understand how the function is carried out, especially with the Scripting API provided by Unity. Unity - Scripting API: Vector3.Lerp

Why do they need the startTime and journeyLength? What is the purpose fracJourney? I don’t understand. I read the description and tried to mess with the code myself but to no avail, I don’t know what they were talking about when they said that “When t = 0 returns a. When t = 1 returns b. When t = 0.5 returns the point midway between a and b.” I’d greatly appreciate it if someone could explain the underlying logic behind the Vector3.lerp, it just isn’t making sense to me.

All it does is interpolate linearly between a and b. The code for Lerp is:

```
public static Vector3 Lerp (Vector3 a, Vector3 b, float t)
{
t = Mathf.Clamp01 (t);
return new Vector3 (a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t, a.z + (b.z - a.z) * t);
}
```

It returns a proportionate mix of **a** and **b**, as determined by t, where 0.0 = 100% **a** and 1.0 = 100% **b.**

–Eric

1 Like

If a is 10 and b is 20, if t is 0 then you get 0% of the way between 10 and 20 = 10.

If t is 0.2, you get 20% of the way between 10 and 20 = 12

If t is 0.5, you get 50% of the way = 15.

And if t is 1.0, you get 100% of the way = 20.

In their example, they’re moving gradually over time between the two positions. They use start time to save the time at some point in a variable, then they use time from that point to create a percentage distance to move between the start and end. They divide the value created by the time by the total length between the two positions they want to travel between to get the percentage to move between the two.

Then they use Lerp to move that percentage.

In other words, they want to increase t (fracJourney in their example) from 0.0 up to 1.0 a little bit each frame to fully move between the two over enough frames.

1 Like