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

Move part from one point to another?

Asked by 5 years ago

Hey, i want to know if there is an way to "animate" one part moving from one part to another without using animations , maybe using :lerp()?

0
If you know about the function lerp then you should know what it does? User#5423 17 — 5y
0
I do but last time i tried to transition CFrame, it did not work, no console output tho... Igoralexeymarengobr 365 — 5y
0
If you're not going to use Lerp(), you can use tweening. DeceptiveCaster 3761 — 5y

2 answers

Log in to vote
1
Answered by
starmaq 1290 Moderation Voter
5 years ago

There are multiple ways to move a part smoothly from a point A to point B. One of them, as you said, it using :lerp(). This function lerps cframes, and it has 2 paramaters: the first being the goal cframe, the cframe value that you wanna hit, or pretty much your destination. And the second, is the percentage of how much of that cframe you want to go. Oh wait, ok that last bit sounds a bit complicated, i'll show you an example. Let's say, we got 2 parts, and we wanna move the first part to the 2nd one. (rememeber that the CFrame is the one that we lerp)

part1.CFrame = part1.CFrame:lerp(part2.CFrame, 0.5)
--So we do it that way, ok so we got the first paramater, which is the goal cframe, what about the percentage, well. That's always gonna be a number between 0 and 1, resembling a percentage. If we do 0.5, that's obviously 50%. Which means right there in that line of code, we will go 50% or pretty much half of that cframe.

let's see that. !enter image description here There, you can see that it goes half of the way. Let's try another percentage.

part1.CFrame = part1.CFrame:lerp(part2.CFrame, 0.2)

!enter image description here

You can see, the goes 20% of the way, or pretty much a fifth.

Now, after you understood how :lerp() works, how do we impelemnt this, so it will go smoothly and look like its slowly going like an animation so it's not instanly heading there. We would use a for loop. Since we have a percentage, we can pretty much for loop the i variable so it slowly goes to 1. And each time it loops we set that 2nd paramater to the i variable.

for i = 0, 1, 0.1 do
  wait()
  part1.CFrame = part1.CFrame:lerp(part2.CFrame, i)
end

So, the first time it loops, i will be 0.1, right? we set the cframe to 10%, then the second time it loops it's 0.2, we lerp 20%, and we go on and on. Until we reach 100%, when we reach that, we pretty much hit the goal cframe. And like that! And of course we would need to add a wait() Let's see our result.

!

There we go! It's moving. If we wanna make this more smoother, we would make the wait() time shorter and increase the increment, which is the 3rd number in the for loop. (an increment means by how much we wanna add). And we make the 2nd part invisible, and we should have a beautfiul moving part.

for i = 0, 1, 0.01 do wait(0.01) 
    workspace.Part1.CFrame = workspace.Part1.CFrame:Lerp(workspace.Part.CFrame, i) 
end

!

0
Remember that there many ways to move a part smoothly, like tweens and body movers, but they all have that feeling of unefficiency somehow. starmaq 1290 — 5y
0
Thank you, that helped me to make an smooth moving brick! Igoralexeymarengobr 365 — 5y
1
np! starmaq 1290 — 5y
Ad
Log in to vote
1
Answered by 5 years ago

As you said, try something like lerping? In this case you can replace part1 and Part2 with whatever you would like. Part1 is the part that is going to move from one part to another, and Part2 would be the end goal (wherever Part1 is going).

local part1 = workspace.Part1
local part2 = workspace.Part2
for i=0,1,0.01 do
    part1.CFrame = part1.CFrame:lerp(part2.CFrame,i)
    wait()
end
0
Thanks, i was trying to lerp, i forgot to add the CFrames, but it isn't really smooth, still heres a upvote :) Igoralexeymarengobr 365 — 5y
0
You can decrease the 0.01 number to make it more smooth (to like 0.001 or whatever you want) crowIess 5 — 5y

Answer this question