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

How can i gradually move a brick?

Asked by 8 years ago

Hey guys i'm wanting to move a brick to the postion of another brick smoothly and gradually, i have created the function below to do this.

The issue with it is that the time it takes to move the brick to the other brick always remains the same because of how low wait() gets and roblox cannot wait with such accuracy and it is rounded to 0.

How do you guys suggest i make this work without making the brick movement seem jumpy, by altering the 0.1.

Thanks for any and all suggestions!

local function move(part,point,speed)
    local distance = (point-part.Position).Magnitude
    local waitTime = distance/speed
    local direction = CFrame.new(part.Position, point).lookVector

    for i = 1, distance, 0.1 do
        part.Position = part.Position + (direction * 0.1)
        wait((waitTime/distance) * 0.1)
    end
end

move(script.Parent, workspace.End.Position, 10)

1 answer

Log in to vote
-1
Answered by
Link150 1355 Badge of Merit Moderation Voter
8 years ago
Edited 7 years ago

Close enough. CFrame:Lerp() basically does under the hood what you were attempting to do yourself.

local destination = CFrame.new(20, 0, 20)
local start = part.CFrame

for i = 0, 1, 0.1 do
    part.CFrame = start:lerp(destination, i)
    wait()
end

Read more about CFrame:lerp() here: http://wiki.roblox.com/index.php?title=CFrame#Methods

and here: http://wiki.roblox.com/index.php?title=Lerp

0
Even after fixing your code, this is too fast and if i increase the wait its too jumpy. jordan0810 55 — 8y
0
Oh, I see. I forgot the 'do' keyword. Yeah, I often do that mistake. Plus, I was kind of in a rush. Updated my answer. Link150 1355 — 8y
0
For an even smoother transition, you can also lower the third value of the 'for' loop. Link150 1355 — 8y
Ad

Answer this question