Mapping or scaling values to a new range

You can use a Lerp/InverseLerp combo:

var result = Mathf.Lerp (10, 100, Mathf.InverseLerp (1, 5, 3));

Or a function I wrote to do this a while ago, which has the advantage of a little better performance in addition to somewhat simpler syntax:

var result = SuperLerp (10, 100, 1, 5, 3);

function SuperLerp (from : float, to : float, from2 : float, to2 : float, value : float) {
	if (value <= from2)
		return from;
	else if (value >= to2)
		return to;
	return (to - from) * ((value - from2) / (to2 - from2)) + from;
}

–Eric

7 Likes