Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How can I use time rather than increment in a lerp function?

Asked by
AZDev 590 Moderation Voter
7 years ago
Edited 7 years ago

Let's say I have a function for Lerping a number.

-- function to lerp number
function numLerp(Start,  Finish , Alpha)
    return Start + (Finish- Start) * Alpha
end

Now I have this function which makes a part become transparent using that numLerp function above.

local Part = workspace.Part

local DesiredTransparency = 1

function numLerp(Start,  Finish , Alpha)
    return Start + (Finish- Start) * Alpha
end

for i = 0, 1, 0.05 do
    wait()
    Part.Transparency = numLerp(Part.Transparency, DesiredTransparency, i)
end

How can I do the same thing but based on time, not increments?

Edit

Okay. I managed to figure this much out.

If we take the number 1 and divide it by the increment which is 0.05 we get the number of increments in the loop which is 20. If we multiply the amount of time between increments (which in this case is roughly 0.3 seconds) by the number of loops which is 20 you get the amount of time it takes for the loop to complete which is roughly 6 seconds. Now I simply need to figure out how to apply this know so that my lerp function is based on time, not increments.

1 answer

Log in to vote
1
Answered by 7 years ago

Now there are few ways how you could do this.

The basic way

The one that would take the least effort and would work nicely with what you have now. This method is basically what you pretty much figured out in the edit. You'd take desired time, divide it with estimated time it takes between iterations and there you have it, approximate iteration count for desired time.

local desiredTime = 4
local waitTime = 0.05
local frames = desiredTime/waitTime

for i=1,frames do
    wait(waitTime)
    local alpha = i/frames
    -- Here you have it! Now you can do some animation things
end

But this way has some drawbacks. This method isn't accurate and it will probably drift over goal time and wait isn't capable of delaying for less than ~1/30 of second(at least that's how it used to work, might have changed).

The advanced way

Now the other method, which I prefer over this, works by animating every available frame, thus it will be as smooth as possible. You keep track of how much time has passed since last frame and use that amount to increase the animation timer, which tracks how long the animation has been playing. Now, since you know current animation playback time and the desired time, getting alpha is as simple as currentTime/desiredTime.

Now, this method will be messier to implement, since you'll have to somehow pass the animation to loop, that will be updating every frame. Since the loop and your game logic will be separate, you'll have to figure out how to make the game know that the animation has finished playing.

local desiredTime = 4

local currentTime = 0
local lastTick = tick()

game:GetService("RunService"):BindToRenderStep("Animations", Enum.RenderPriority.Camera.Value + 1, function()
    local newTick = tick()
    local delta = newTick - lastTick -- Delta is a fancy term for how much a variable has changed
    lastTick = newTIck

    currentTime = currentTime + delta

    local alpha = currentTime/desiredTime

    -- Here you can do your animation magic
end)

Now that is a simple example of how it would work. You'd still need to figure out how you want to queue the animations and remove them once they are over.

0
A few questions. What is renderpriority and why is it set 1 above camera? When the animation is complete can I not unbind from renderstepped or disconnect or something? @ZarsBranchkin AZDev 590 — 7y
0
Bind to renderstep registers a function that should get called every frame. Render priority determins when that function should get called. Might want to play around with that, but it seemed that running it after updating camera was a good idea. You can unbind, but I'd say it's better idea to leave it and update all animations with that function. The way I usually do that I have a table where I (. ZarsBranchkin 885 — 7y
0
(..) table where I put the "animations"(tables) that should play. These tables contain animation's duration, it's current playback time and a function that does the animation and accepts the alpha as argument. Once animation is over, it gets removed from animation table. ZarsBranchkin 885 — 7y
0
Alright thanks a lot. @ZarsBranchkin AZDev 590 — 7y
Ad

Answer this question