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()?
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)
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
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