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

Smoothly change the size/position of a Part?

1 answer

✓ Accepted answer

Using Linear Interpolation

Linear Interpolation, or 'lerp', is quite simple once you get the jist of it. Lerp is finding a value between a starting and ending point, linearly. The forumla for lerp is a + (b - a) * t. Where a is your starting point, b is your ending point, and t is a number between 0 and 1. Think of 't' as a percent, but instead of 0 - 100 it's 0 - 1. So using .5 as 't' would result in the halfway mark between 'a' and 'b'.

So, how would you use lerp to smoothly move one value to the next? a numerical for loop.

Have the start of the for loop, the starting number, and the end the ending number. Then you can use i/ending_number for the 't' value.

function lerp(a,b,t)
	return a + (b - a) * t
end

local a = 5
local b = 30

for i = a,b do
	c = lerp(a,b,i/b)
	print(c)
end

Applying Lerp to Position

Pretty much all you have to do is use lerp on the X,Y, and Z values of both the starting Vector, and the ending Vector

function plerp(a,b,t) --'Position lerp' c;
	local x = a.X + (b.X - a.X) * t --X
	local y = a.Y + (b.Y - a.Y) * t --Y
	local z = a.Z + (b.Z - a.Z) * t --Z
	return Vector3.new(x,y,z) --Make a vector out of all the values
end

local a = Vector3.new(10,100,-5)
local b = Vector3.new(-6,20,50)

for i = 1,200 do --200 just cuz
	local pos = plerp(a,b,i/200)
	wait()
end

Applying Lerp to Size

It's the same thing as applying it to Position - but as you may know, Changing the size of a part makes it's size linearly go out in both directions of whatever axis you're resizing. To make up for this, we need to find the difference of what the size was divided by two - then offset the position on whatever axis you're resizing by this difference.

function slerp(a,b,t) --'Size Lerp' c;
	local x = a.X + (b.X - a.X) * t
	local y = a.Y + (b.Y - a.Y) * t
	local z = a.Z + (b.Z - a.Z) * t
	return(x,y,z)
end

local part = workspace.Part
local a = Vector3.new(10,10,10)
local b = Vector3.new(5,30,2)

for i = 1,200 do
	local before = part.Size
	local size = slerp(a,b,i/200)
	local dist = size - before
	part.Size = size
	part.CFrame = part.CFrame * CFrame.new(-dist.X/2,-dist.Y/2,-dist.Z/2)
	wait()
end

4 comments

0

Very advanced. I'm not sure if I'll be able to tackle this, but thank you anyways!

0

What is a,b,t

0

I already explained that at the top of my answer

0

Silly me, I didn't see :)

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