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