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

How should i use :Lerp() right so this problem won't occur?

Asked by
imKirda 4491 Moderation Voter Community Moderator
3 years ago

Well so i know what is lerping and how to use it, i use it to move a gate but i have a problem with it, here is an example script:

for index = 0, 1, 0.001 do
    gate.CFrame = gate.CFrame:Lerp(end_point.CFrame, index)
    wait(0.0029)
end

So this is what i've seen everyone on devforums but the thing is that it does not lerp how i want it to, if i add print(index) to it, then it prints the index and when the index is at 0.1 the gate is already lerped at that position, i know the reason why because it lerps from gate's position that has already lerped bit forward but how can i fix it? I have made it so it breaks the loop when it hits 0.1 but is there any other way?

Thanks for spending time.

0
Do you want the end position of the for loop to be 0.1? Your explanation is vague, and I see nothing wrong with your code. Dovydas1118 1495 — 3y

1 answer

Log in to vote
1
Answered by
gskw 1046 Moderation Voter
3 years ago

The window within which the lerp occurs shrinks on every iteration, because it's not saved anymore. Let's say that your lerp progress goes from 0 to 1000. On the first iteration, index is 0, and nothing changes. On the second iteration, index is 0.001, and the lerp progress is updated to (1000 - 0) * 0.001, still as expected. However, on the next iteration, it's as if your lerp progress goes from 1 to 1000, so the window has shrunk. See the problem?

    -- gate.CFrame keeps getting closer to end_point.CFrame, so lerping it by 0.5
    -- or whatever value means a different thing on each iteration.
    gate.CFrame = gate.CFrame:Lerp(end_point.CFrame, index)
    wait(0.0029)

The solution is to store the original CFrame in a variable, and lerp that:

local start_point = gate.CFrame
for index = 0, 1, 0.001 do
    gate.CFrame = start_point:Lerp(end_point.CFrame, index)
    wait(0.0029)
end

If you have any questions about my explanation, please leave a comment.

0
Ooh it was that easy, thanks. imKirda 4491 — 3y
Ad

Answer this question