- Home /
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
If you want to stretch out inverselerp so 0->-1, 0.5->0, and 1->1 use either tigertrussell's code or something similar to illogicalcrows (just replace lerp with inverselerp).
If you want to keep the accurate values but relax the clamp so InverseLerp is capable of return -1 in instances such as InverseLerp(0, 2, -2) use maccabbe's code.
Answer by illogicalcrow · Mar 15, 2015 at 03:34 AM
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;
Answer by tigertrussell · Mar 15, 2015 at 03:34 AM
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.
You probably ment:
float specialLerp = $$anonymous$$athf.Lerp(-1, 1, originalLerp);
Answer by maccabbe · Mar 15, 2015 at 03:35 AM
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;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613190157im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Finding coordinates based on an angle and distance? 3 Answers
How to find inverse cos 1 Answer
Mathf.Approximatley if statement 0 Answers
Mathf clamp movement to Camera space in js 1 Answer
How to get a value between 0 and 1 2 Answers