# How can I have a distance between 2 points = 0-1 (min = 0, max = 1) and in between is a decimal? [C#]

Basically I have 2 points, min and max. I also have another point that can move in between the 2 other points. I want it to where if the point is at min, a variable = 0, if it is in the center, it = 0.5, if it is at max, it = 1, etc. The positioning of min and max will change too, so it needs to stay updated.

if by “points” you mean Vectors (unity’s term for positions in space. Then what you want to do it check the distances

Vector3 minPoint;
Vector3 maxPoint;

Vector3 thisPoint

float distanceFromMin = Vector3.Distance(thisPoint, minPoint);
distanceFromMin = Mathf.Round(distanceFromMin) this will give you a value 0.0f - 1.0f;

Just repeat this for the maxPoint as well

What you want is InverseLerp but for a Vector3. Unfortunately Vector3 doesn’t have such a function. However if the point is exactly on the line between the two points you can use any of the 3 components as long as the components are not the same. So for example If you have (0,0,0) as start point and (10,0,5) as end point you can use x or z but not y as it is the same in start and end. So you could do

``````float t = Mathf.InverseLerp(start.x, end.x, point.x);

// or

float t = Mathf.InverseLerp(start.z, end.z, point.z);
``````

However a more general approach would be projecting the relative vector from start to point onto the vector start to end.

``````Vector3 dir = end - start;
float t = Vector3.Dot(point - start, dir) / dir.sqrMagnitude;
``````

Note that this generally works beyond the borders of start and end. So if “point” is before start the value will be negative, if it’s behind end it will be larger than 1