Hi all, searched around a bit and didn’t find anything, but certainly could have missed it.
Wondering if there’s a built-in function which can take a value and scale/map it to a different range, where you can set the input and output ranges. For example:
if you send in 1, you’ll get 10; if you send in 3, you’ll get 60; if you send in 5, you’ll get 100.
So it’s an offset-plus-scale, all in one. I suppose you can just roll your own each time, but wondering if it exists already as a function (and maybe with exponential scaling?)
I know other languages have this, just wondering if it was something obvious I missed…maybe some trick with Lerp?
Ah, that’s it! I had that formula around somewhere (from Max/MSP “scale” object) but couldn’t remember it exactly. Same as processing’s “map” function. Might be nice to have it as a built-in function in Unity like “Mathf.Map” or something.
Interesting about the Lerp/InverseLerp combo idea too, I’ll have to experiment with that.
Thanks Eric5h5, I needed that as well. I rewrote the function in C# and called it Map for simplicity (hopefully not affecting any reserved names, though it seems to work fine).
Hey @PeterWilkinson ,
Somehow the code above didn’t work for me, but the actual Arduino map function works well with minor modification (changing long datatype to double) - at least in my application.