LerpUnclamped
Interpolates linearly between two vectors, allowing extrapolation beyond the end points.
Read time 4 minutesLast updated 4 days ago
Definition
- Type: Method
- Namespace: UnityEngine
- Assembly: UnityEngine.CoreModule
LerpUnclamped(Vector3, Vector3, float)
Interpolates linearly between two vectors, allowing extrapolation beyond the end points.
public static Vector3 LerpUnclamped(Vector3 a, Vector3 b, float t)
Parameters
Returns
Type | Description |
|---|---|
| Vector3 | Interpolated value. This value always lies on a line that passes through points |
Remarks
Unclamped linear interpolation allows for values of less than zero or greater than one, such that the returned vector can lie outside of the points and .
tabThis method is useful for finding a point some constant rate along the line through two endpoints. For example, to move an object gradually beyond and through those points.
The returned value V equals
V = A + (B − A) × t
The method interpolates between points and , such that:
ab- When ≤ 0, this method returns a vector that points along the line through
tandabut in front ofb.a - When 0 < < 1, this method returns a vector that points along the line between
tanda. The distance along the line corresponds to the fraction represented byb.t - When > 1, this this method returns a vector that points along the line through
tandabut beyondb.b
// This example creates three primitive cubes. Using linear interpolation, one cube moves along the line that passes through the others.// Because the interpolation is not clamped to the start and end points, the moving cube passes the end cube after the interpolation frame limit is reached. // Attach this script to any GameObject in your scene. using UnityEngine;public class LerpUnclampedExample : MonoBehaviour{ // Number of frames in which to completely interpolate between the positions int interpolationFramesCount = 300; int elapsedFrames = 0; // Number of frames to reset the moving cube to the start position int maxFrameReset = 900; GameObject CubeStart; GameObject CubeEnd; GameObject CubeMove; void Start() { // Create the cubes CubeStart = GameObject.CreatePrimitive(PrimitiveType.Cube); CubeStart.transform.position = new Vector3(-5,0,0); CubeEnd = GameObject.CreatePrimitive(PrimitiveType.Cube); CubeEnd.transform.position = new Vector3(5,0,0); CubeMove = GameObject.CreatePrimitive(PrimitiveType.Cube); CubeMove.transform.position = CubeStart.transform.position; } void Update() { float interpolationRatio = (float)elapsedFrames / interpolationFramesCount; // Interpolate position of the moving cube, based on the ratio of elapsed frames CubeMove.transform.position = Vector3.LerpUnclamped(CubeStart.transform.position, CubeEnd.transform.position, interpolationRatio); // Reset elapsedFrames to zero after it reaches maxFrameReset elapsedFrames = (elapsedFrames + 1) % (maxFrameReset); }}
Additional Resources: Vector3.Lerp Vector3.SlerpUnclamped
LerpUnclamped(Vector3, Vector3, float)
Interpolates linearly between two vectors, allowing extrapolation beyond the end points.
public static Vector3 LerpUnclamped(in Vector3 a, in Vector3 b, float t)
Parameters
Returns
Type | Description |
|---|---|
| Vector3 | Interpolated value. This value always lies on a line that passes through points |
Remarks
Unclamped linear interpolation allows for values of less than zero or greater than one, such that the returned vector can lie outside of the points and .
tabThis method is useful for finding a point some constant rate along the line through two endpoints. For example, to move an object gradually beyond and through those points.
The returned value V equals
V = A + (B − A) × t
The method interpolates between points and , such that:
ab- When ≤ 0, this method returns a vector that points along the line through
tandabut in front ofb.a - When 0 < < 1, this method returns a vector that points along the line between
tanda. The distance along the line corresponds to the fraction represented byb.t - When > 1, this this method returns a vector that points along the line through
tandabut beyondb.b
// This example creates three primitive cubes. Using linear interpolation, one cube moves along the line that passes through the others.// Because the interpolation is not clamped to the start and end points, the moving cube passes the end cube after the interpolation frame limit is reached. // Attach this script to any GameObject in your scene. using UnityEngine;public class LerpUnclampedExample : MonoBehaviour{ // Number of frames in which to completely interpolate between the positions int interpolationFramesCount = 300; int elapsedFrames = 0; // Number of frames to reset the moving cube to the start position int maxFrameReset = 900; GameObject CubeStart; GameObject CubeEnd; GameObject CubeMove; void Start() { // Create the cubes CubeStart = GameObject.CreatePrimitive(PrimitiveType.Cube); CubeStart.transform.position = new Vector3(-5,0,0); CubeEnd = GameObject.CreatePrimitive(PrimitiveType.Cube); CubeEnd.transform.position = new Vector3(5,0,0); CubeMove = GameObject.CreatePrimitive(PrimitiveType.Cube); CubeMove.transform.position = CubeStart.transform.position; } void Update() { float interpolationRatio = (float)elapsedFrames / interpolationFramesCount; // Interpolate position of the moving cube, based on the ratio of elapsed frames CubeMove.transform.position = Vector3.LerpUnclamped(CubeStart.transform.position, CubeEnd.transform.position, interpolationRatio); // Reset elapsedFrames to zero after it reaches maxFrameReset elapsedFrames = (elapsedFrames + 1) % (maxFrameReset); }}
Additional Resources: Vector3.Lerp Vector3.SlerpUnclamped