Mahtf.inverselerp

Hey so i have a simple question

can i get mathf.inverselerp to return a value between -1 and 1 instead of 0 and 1?

Thanks ~Scott

Multiply by 2 and subtract 1. A little less precise, but it will pretty much never matter. Overall this kind of conversion is a common pattern.

value = 2 * Mathf.lerp(from, to, t) - 1;

Would this work?

float originalLerp = Mathf.InverseLerp(a, b, x);
float specialLerp = Mathf.Lerp(-1, 1, originalLerp);

I think specialLerp holds what you’re looking for.

Lerp(from, to, x) = from(1-x)+to*x
lerp-from=x(to-from)
(lerp-from)/(to-from)=x

so define your own function as

public float InverseLerp(float from, float to, float value){
    if(to!=from){
       return Mathf.Clamp((value-from)/(to-from), -1f, 1f);
    }
    return 0;
}