Hi
I’m trying to remap a value from one range to a relative number from a different range.
eg source range = 0 to 100
target range = -10 to 10
so source value of 0 converts to -10, 100 converts to 10 and all in between to the relative points
Something like:
int value = 50;
int NewValue = convert(value,0,100,-10,10);
Can’t you create 2 variables and just change those? For example, if I want a random number from a certain dynamic range, I can do this:
public int minRange;
public int maxRange;
int rand;
void SetRand()
{
rand = Random.Range(minRange, maxRange);
}
ljarbo
4
If you use the length of each range, the problem is quite easy.
one range of 100 and another with the range of 20.
for example:
23 of 100 is 23%. (23/100 = 0.23)
your other range has a range of 20.
23% of 20 is 20*0.23= 4.6
and then you adjust for the negative starting point. 4.6 - 10 = -5.4
@ljarbo, that’s genius. I don’t know how I missed that thank you
If you want to do this as a general case, rather than hard-coding the math, use a Lerp/InverseLerp combo.
int value = 50;
int newValue = Mathf.Lerp (-10, 10, Mathf.InverseLerp (0, 100, value));
–Eric