How it works...

We are using the Lerp() function in two ways:

  • Standard method: The Lerp() function's float t parameter has values from 0 to 1. We've used a timer as out t parameter. The timer first was increasing to the value of 1. After it reached 1, it was decreasing to 0. This method gives us standard linear interpolation between two values. The t parameter is the percentage of the interpolation (for t = 0.5, the interpolated value is an average of two input values).
  • Continuous method: The second method is based on using the output value (in our case the position) as the lower input value in the Lerp() function. We call the Lerp() function in Update() and use Time.deltaTime as the t parameter. Time.deltaTime is a very small value, so each time the function is called, the output value is interpolated toward the end value by a very small fraction. Calling this method in Update() results in smooth movement. It is not linear anymore. The BlueDot moves faster if it is further away from the cursor and slows down when it gets closer.
Remember that when you're using the second method, the interpolated value will never truly get to the end position or end value. That's why sometimes we have to implement an if statement and check if the interpolated value is close enough to the end value. If so, we can set it to be exactly the same.

The  Lerp() function is implemented in various classes. These are the most commonly used examples:

  • Mathf.Lerp(): This interpolates between two float values
  • Vector3.Lerp():This interpolates between two vectors (positions for instance)
  • Quaternion.Lerp():This interpolates between two rotations
  • Color.Lerp():This interpolates between two colors

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.40.32