2

I have three vector3 variables; the start, the end and some point in-between. I know in normal linear interpolation you give the start and end points along with a 't' variable such as "0.6" which returns the position at that point along the path.

What i want to do is pass the start and end point along with a position and get back the 't' value, so for example i might pass in a position just over half way and get back '0.6'.

I know this is a bit of a strange one and I've probably done a poor job explaining it, but any help would be much appreciated.

user414025
  • 105
  • 1
  • 10
  • 1
    i think i solved it, by just using the start and end as a number range and then scaled it down to between 0 and 1 – user414025 Sep 11 '18 at 16:44

2 Answers2

2
public float ReverseLerp(Vector3 start, Vector3 end, Vector3 point)
    {
        float tx = (1f - 0f) / (end.x - start.x) * (point.x - end.x) + 1f;
        float ty = (1f - 0f) / (end.y - start.y) * (point.y - end.y) + 1f;
        float tz = (1f - 0f) / (end.z - start.z) * (point.z - end.z) + 1f;
        float totalClamp = tx + ty + tz;

        float weight = (1f - 0f) / (3f - 0f) * (totalClamp - 3f) + 1f;
        weight = ((weight - 0f) / (1f - 0f)) * (1f - 0f) + 0f;
        weight = Round(weight, 2);
        return weight;
    }
user414025
  • 105
  • 1
  • 10
0

If you can make the ASSUMPTION that the third point is on the line between the first two:

float MagA = (Position2 - Position1).Magnitude;
float MagB = (LerpedVector - Position1).Magnitude;
float fraction =  MagB/MagA;
Glurth
  • 290
  • 1
  • 17