- Home /

# 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.

**Answer** by FireStone720
·
Aug 16, 2018 at 11:06 PM

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

I don't quite understand your logic here. distanceFromMin would just be the worldspace distance rounded to the nearest integer and not a decimal between 0 and 1.

So if "min" is (1,1,1), point is (5.5.5) and max is (10,10,10) your solution woulb give you the value "7" for "distanceFromMin " and a value of "9" for distanceFromMax.

Also note that a vector is not a "Unity term".

**Answer** by Bunny83
·
Aug 16, 2018 at 11:16 PM

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

With the first method, how could I get it to where everywhere in between is a decimal, right now, it is if it is at min, it is 0, and max it is 1, which is right, but there is no inbetween.

I don't quite get your question. Just imagine those values:

```
start = (10,5,3)
end = (20,0,3)
point = (12,4,3)
```

When you use the first method on either x or y it will return "0.2". Keep in mind that the first method only works when the point is exactly on the line that connects start and end. The second method actually projects the point onto the line orthogonally

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.