Is it possible to turn a value in to a different value between 2 numbers?

For example, I get a number with a value slowly going from 0 to X. Would it be possible for the final value to be 0.75 to 1?

Another example that might make more sense:

If the current value is 0.5, the expected value should be 0.875, if the current value is 0, the expected value should be 1, and if the current value is 320, the expected value should be 1.

I understand if this makes completely 0 sense, but I appreciate any help!

Do you mean like remapping values, in terms of lerp?

I’m confused to what you mean but this is the sort of logic I’d see someone try to explain if they were looking at the lerp remap function.

-- 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

Credits to CodeOtaku

1 Like

Not quite sure if that’s the solution, I’ll check it out in a sec, but I’ll go in to depth with what I’m trying to do.

Basically, I am making a horror game and I’m currently creating the Heartbeat effect system so that when you go closer to the entity, the more the effects will show. And the last effect will be just a red frame tweening its transparency (sort of like Apeirophobia). I want the BackgroundTransparency of the frame to be between 0.75 and 1, based off of the Heartbeat SFX’s PlaybackLoudness. I am just trying to figure out to get the value.

Ah yes, you’ll need the lerp function then for the 0.75 to 1

local frame = script.Parent
local sound = workspace.Sound
local rs = game['Run Service']

local beatDownAggressiveness = 0.1
local beatUpAggressiveness = 0.9

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

function inverseLerp(a, b, x)
	return math.clamp((x-a)/(b-a), 0, 1)
end

local lastT = 0

RunService.Heartbeat:Connect(function(deltaTime)
       local t = inverseLerp(0, 1000, sound.PlaybackLoudness)

	if t >= lastT then
		t = lerp(lastT, t, beatUpAggressiveness))
	else
		t = lerp(lastT, t, beatDownAggressiveness)
	end

        frame.BackgroundTransparency = lerp(0.75,1,t)
	lastT = t
end)
1 Like

Tried out the script before the edit and it worked perfectly, exactly what I was looking for! Thank you!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.