static float InverseLerp( Vector3 pos, Vector3 a, Vector3 b, bool clamp )

book_4_sparkGenerated
code_blocksInput

Description

The InverseLerp method calculates the linear parameter t that produces the interpolant pos within the range defined by vectors a and b. This is essentially the inverse operation of a linear interpolation (Lerp), where you determine the position of a point within a line segment.

Usage

Use this method when you need to find the relative position of a point along a line segment defined by two vectors. The method returns a value between 0 and 1 if the point lies between the two vectors. If the clamp parameter is set to true, the result will be clamped to the range [0, 1].

Example

Vector3 point = new Vector3(5, 5, 5);
Vector3 start = new Vector3(0, 0, 0);
Vector3 end = new Vector3(10, 10, 10);

float t = Vector3.InverseLerp(point, start, end, true);
// t will be 0.5, as the point is halfway between start and end.