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

Why is this CFrame:lerp() for loop not working properly?

Asked by 7 years ago

I have a very simple :lerp() for loop that moves a part from 0,0,0 to 100,0,0 over 10 seconds:

part = game.Workspace.Part

for i = 0,1,0.1 do
    part.CFrame = part.CFrame:lerp(CFrame.new(100,0,0),i)
    print(i, " ", part.CFrame.p)
    wait(1)
end

Now, if the code was to work properly, the part would have moved the block 10 studs every second. Instead, the part moves at a nonlinear pace. Output:

0 0, 0, 0

0.1 10, 0, 0

0.2 28, 0, 0

0.3 49.6000023, 0, 0

0.4 69.7600021, 0, 0

0.5 84.8800049, 0, 0

0.6 93.9520035, 0, 0

0.7 98.1856003, 0, 0

0.8 99.6371155, 0, 0

0.9 99.9637146, 0, 0

1 100, 0, 0

So, instead of moving at the linear pace of 10 studs a second like it should, the part is instead moving at a nonlinear, fluctuating pace, and I don't know why.

1 answer

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago

This is because your starting CFrame for the lerp function is going to be updated every iteration..

Predefine a variable, and go off of that.

local part = workspace.Part
local priorCF = part.CFrame --Predefine a variable!

for i = 0,1,0.1 do
    part.CFrame = priorCF:lerp(CFrame.new(100,0,0),i)
    print(i, " ", part.CFrame.p)
    wait(1)
end
0
Thanks! That works! jamesarsenault 192 — 7y
Ad

Answer this question