What we did earlier was simply create a Move () method and set a position.
It is true that with Time.time the hope moves constantly, but using a simple float the sphere “finds” its position between Point A and Point B.
To create a linear interpolation instead, we have to make the sphere move from Point A to the desired point.
To do this, we will create a Lerp() method from scratch.
The reference for the Vector3.Lerp method is this
As we can see from the description,
t =0 represents point A,
t =1 represents point B
and t = 0.5 represents the halfway point between point A and point B.
We now know that the Lerp method requires two Vectors, A and B, and a float t.
The formula, as the documentation says, is this
a + (b - a) * t
We can also read this formula like this
a + (length of b - a) multiplied by a float t
So far everything pretty simple right?
Let’s translate it into code
first, we need the distance from Point B to Point A
After this, let’s translate the formula
The last piece of our code is to set the t as CLAMP
The reference for the MathF.Clamp
The clamp method requires three parameters: one float for a value and two floats for a minimum and a maximum value.
The first value is none other than value of t, and the Clamp will be between 0 and 1.
And return the new Vector3
If we now set the Lerp() in Start()
If we test it
The sphere moves from point A but stops when it reaches point B.
In the next articles we start talking about Planes.