Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Brick tweening (Lerp)?

Hi, I have been trying to figure out how to tween a brick using the lerp method. The wiki is no help, can you help me understand thus a little more?

2 answers

✓ Accepted answer

Vector3:Lerp takes two args, where the end position is, and the alpha. The alpha is a number between 0 and 1. It's how far between the start and end point the returned value is.

To get the value between two values we would do

Vector3.new(0, 0, 0):Lerp(Vector3.new(0, 10, 0) -- Vector3.new(0, 5, 0)

So if you want a value to tween(interpolate, lerp, ease, they all mean relatively same thing) between two points you would use a loop

local start = Vector3.new(0, 0, 0)
local finish = Vector3.new(0, 10, 0)
for i = 0, 1, .1 do
	brick.Position = start:Lerp(finish, i)
	wait()
end

It's also worth noting that Lerp means linear interpolation. It takes a straight path from start to end, it uses the formula a + (b - a) * alpha

There's a lot of different formulas to tween, linear is the most basic but there's a lot better ones to use for smooth transitions.

3 comments

1

You should change the variable 'end' to 'EndPos' etc., because end conflicts with loops, functions etc (constructive criticism).

0

"start" and "finish" are good pairs (since end is reserved you can't use "start" and "end", as TheDeadlyPanther points out)

0

Thanks pointing that out, edited :)

local PartYouWantToMove = game.Workspace.Part;
local framesItWillTakeToFinish = 30;
local finalCFrame = CFrame.new(0, 0, 0)
local finalCFrameAngles = CFrame.Angles(math.rad(0), math.rad(0), math.rad(0))

for i = 1, framesItWillTakeToFinish do
PartYouWantToMove.CFrame = PartYouWantToMove.CFrame:Lerp(finalCFrame*finalCFrameAngles), i*1/framesItWillTakeToFinish)
game["Run Service"].RenderStepped:wait()
end

Okay, so here, the framesItWillTakeToFinish is what it says. usually, youll get 60 fps, so if you set it to 30, then it will take 0.5 seconds for the part to go from its original position to get to its final position. The final cframe is where you want the part to go (position), and the finalCFrameAngles (along with the math.rad() to convert it to degrees) will let you tell the script what angle you want the part to be at once its completed. the game["Run Service"].RenderStepped:wait() is a cheeky way to make a wait(), but instead of 1/30th of a second, its one frame (so like 1/60th of a second.) The last number is how far along the part is along its lerp, so 0 will be the beginning and 1 will be the end. You always want this to match up with how many times you will run the for i = 1, ----- stuff.

This discussion is preserved as it was. New answers, comments, and votes are closed.