Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I incorporate elapsed time into linear interpolation?

I'm updating a value 60 percent closer to it's target value every frame using the formula a+(b-a)*alpha. This works, but if someone has low frames it'll take longer to reach it's target.

local target = 4
local value = 0

local last = tick()
while rs:wait() do
local time = tick()
	local elaps = time - last
	value = value + (target - value) * .6
end

How can I modify this formula to use the elapsed time?

1 comment

0

You could probably just multiply by elaps*30

1 answer

✓ Accepted answer

Oh that's simple, but completely silly.

The issue is that in this equation, you're missing a fundamental scale of when the tween is supposed to finish. Are we supposed to go there in 2 seconds? 2 minutes? It's all about that end goal.

local target = 4
local value = 0

local last = tick()
while rs:wait() do
local time = tick()
    local elaps = time - last
    last = time
    value = value + (target - value) * (1-0.6^elaps)
end

4 comments

0

I probably should've included a note on this; I know it will never finish. Target is constantly changing, I'm making a smooth transition between value changes

0

Then the time is irrelevant. If you have no target, your time is completely irrelevant.

0

I have a target, it is dynamic. Currently I'm moving value 60% closer to target every frame. If someone has 30 fps, their value will be far behind someone's who has 60 fps (Assuming they start at the same value and have the same target).

0

Right that's what you want. You'll need the delta, which is magic stuff might I add. Check my revised code.

This discussion is preserved as it was. New answers, comments, and votes are closed.