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

Brick tweening (Lerp)?

Asked by 8 years ago

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

Log in to vote
1
Answered by 8 years ago

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.

1
You should change the variable 'end' to 'EndPos' etc., because end conflicts with loops, functions etc (constructive criticism). TheDeadlyPanther 2460 — 8y
0
"start" and "finish" are good pairs (since `end` is reserved you can't use "start" and "end", as TheDeadlyPanther points out) BlueTaslem 18071 — 8y
0
Thanks pointing that out, edited :) YellowoTide 1992 — 8y
Ad
Log in to vote
0
Answered by 8 years ago
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.

Answer this question