I came up with a useful little function that I’ve used in my game many times. Basically, it defines a linear relationship between two number ranges.
function ConvertScale(input,inputRange,outputRange,noClamp)
if input and inputRange and outputRange then
local inputRangeMin = inputRange[1]
local inputRangeMax = inputRange[2]
local outputRangeMin = outputRange[1]
local outputRangeMax = outputRange[2]
local scale = (input - inputRangeMin) / (inputRangeMax - inputRangeMin)
local unclampedOutputValue = ((outputRangeMax - outputRangeMin) * scale) + outputRangeMin
local clampedOutputValue = math.min(outputRangeMax,math.max(outputRangeMin,unclampedOutputValue))
return (noClamp and unclampedOutputValue) or clampedOutputValue
end
end
Examples:
Changing the transparency of a brick depending how high it is off the ground. In this example, if the block is touching the ground (position.y == 0), it will be nearly invisible. If the block 100 studs or higher, it will be opaque. If the block’s height is at around 50 studs, the block’s transparency will be slightly below .5
local heightRange = {0,100} --range from the ground to 100 studs in the air
local transparencyRange = {.95,0} --nearly invisible to opaque
local transparency = ConvertScale(Block.Position.Y,heightRange,transparencyRange)
Block.Transparency = transparency
Changing the volume of in-game music depending on the position of a gui volume slider
local musicVolumeRange = {0,1.5} --let's say the max volume of the music is 1.5
local sliderPositionOffsetRange = {0,500} --let's say the slider's range of movement is 500 pixels wide
local volume = ConvertScale(SliderObject.X.Offset,sliderPositionOffsetRange,musicVolumeRange)
MusicSoundObject.Volume = volume --If the slider's offset is 500, then the music volume will be 1.5
You can pass “true” through the noClamp argument if you want to scale the output value beyond the output range.