Change a number to a goal number in a pre-determined amount of time?

Sorry for the second post the first one wasn’t clear.

Hello, i’m looking for a way to change a number to another number progressively in a pre-determined amount of time. I’m kinda bad at maths, if someone could help me find out a formula that would be great. Thank you very much.

local CurrentY = 0.5
local GoalY = 11.5 
local Lenght = 2
while task.wait() do
	-- I'm looking to change the CurrentY to GoalY in 2 seconds, without lerp/Tween
    CurrentY = ? 
end

You cannot do this accurately without interpolation. Lerping is the easiest option.

--use this function
function lerp(a, b, alpha)
    return a + (b - a) * alpha
end

This should work like this:

local ZeroY = 0.5
local CurrentY = 0.5
local GoalY = 11.5 
local Lenght = 2

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

local t = 0

while true do
	-- I'm looking to change the CurrentY to GoalY in 2 seconds, without lerp/Tween
    CurrentY = lerp(ZeroY, GoalY, t/Lenght)
    t += task.wait()
end

Not completely sure if I remembered the lerp function right.

1 Like

I think that’s what you’re asking for:

--Params
local CurrentY = 0.5
local GoalY = 11.5
local Length = 2

--Calculations
local frameTime = task.wait()
local n = Length/frameTime
local dt = (GoalY-CurrentY)/n

CurrentY += dt 

--Custom tween
for i = 1, n-1 do
	CurrentY += dt
	task.wait(frameTime)
end
3 Likes

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