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