Interpolation (Lerps, SLerps, and more)

From Unity3D

Jump to: navigation, search

This section includes various methods for translating vectors; the functions herein can be used to affect the position, rotation, and scale (among other things) of a GameObject over time.

To calculate a linear interpolation between same-sized Vectors (Vector2, Vector3, or Vector4), you can use the Lerp function of that Vector object. Note that this function does not modify either of its Vector parameters, but simply returns a new Vector that has values appropriately interpolated in accordance with a provided ratio (0-1).

The following code uses Lerp to translate the position of an object 10 units forward over the course of 5 seconds (starting immediately when the game is run). This would be placed in the "Update" method of a script:

transform.position = Vector3.Lerp(new Vector3(0,0,0),new Vector3(0,0,10),Time.time/5);

See the Script Reference for the function for more information:

Furthermore, all three types of Vectors have a MoveTowards function. Like Lerp, this function returns a new Vector. However, instead of calculating an interpolation of the two Vectors, it simply "moves" the first Vector toward a target Vector at a given speed. Thus, where Lerp calculates based on a ratio toward the target, MoveTowards calculates based on a set speed.

See the Script Reference for the function for more information:

Notably, Vector3 (and not Vector2 or Vector4) also has a Slerp (Spherical Interpolation) and RotateTowards function (which is what MoveTowards is to Lerp). These functions are typically used in Quaternion math: if you are unfamiliar with Quaternion math, these functions may be unintuitive. In short, this function interpolates the Vector as if it were traveling across the surface of a sphere using the shortest path possible.

See the Script Reference for these two functions for more information:

Lastly, Vector3 (and not Vector2 or Vector4) also has a function called SmoothDamp that can be used to interpolate a Vector over time. It is used in a very similar fashion to the Lerp and Slerp functions, but incorporates a certain amount of smoothing. In order to do this, it requires a velocity parameter that is a reference to an existing Vector3: this Vector will be modified throughout the entirety of the function's execution, and thus must exist "permanently" in memory (so that it can be modified). Furthermore, instead of providing a 0-1 ratio value, this method takes in a time (in seconds) of how long the function should take to complete its translation. Note that this time is an approximation: the function can easily take more or less time, and thus is not very precise.

For more information on the use of this function, see its Script Reference:

Aside from the functions built-in to Vectors, the Mathf class has a few functions (including lerping functions) that can be used for interpolating numeric values between a starting and ending value:

For more information on these and other properties of the Mathf class, see its Script Reference:

Personal tools