Linear interpolation (Lerp, InverseLerp, Remap) Video Tutorial

Lua Scripting, Linear interpolation (Lerp, InverseLerp, Remap) - YouTube
Code made in the tutorial:

-- Domain: t[0->1]
-- Range:   [a->b]

local function lerp(a, b, t)
  return a + (b-a) * t
end

-- Domain: x[a->b]
-- Range:   [0->1]
local function inverseLerp(a, b, x)
  return (x-a)/(b-a)
end

-- Domain: [in1  ->  in2], t[0->1]
-- Range:  [out1 -> out2]
local function remap(in1, in2, out1, out2, x)
  return lerp(out1, out2, inverseLerp(in1, in2, x))
end

-- Real world example
local slider = {0, 100}
local sliderValue = 50
local playerSpeed = {16, 64}

print(remap(slider[1], slider[2], playerSpeed[1], playerSpeed[2], sliderValue))
5 Likes

I made this illustrations myself for the video
Feel free to save them and use them!
Credits are appreciated but not required :smiley:

Remap function:

This is really cool I enjoyed it. BTW Iā€™m previously Medono - Roblox Videos now Coded_Tom.

1 Like