You are not logged in.
Pages: 1
A tiny situational script to scale a value from one range to another
/// scale_val(val, in_min, in_max, out_min, out_max)
//
// Scale a value from one range to another
//
/// GMLscripts.com/license
function scale_val(val, in_min, in_max, out_min, out_max) {
if (in_min == in_max) return out_min;
var in_len = in_max - in_min
var out_len = out_max - out_min;
var val0 = clamp(val, in_min, in_max) - in_min;
var in_percent = abs(val0 / in_len);
return in_percent * out_len + out_min;
}
Explaination:
scale_val(75, 50, 100, -10, 10) returns: 0
75 is 50% between 50 and 100 therefore 50% between -10 and 10 is 0
scale_val(25, -25, 25, 0, 100) returns: 100
scale_val(-25, -25, 25, 0, 100) returns: 0
Last edited by maras (2024-02-15 15:54:01)
I'm on the official GM discord > maras_cz
Offline
Hi, maras. Thanks for suggesting this. We have a script similar to this already.
Abusing forum power since 1986.
Offline
Pages: 1