Anyone know how to make a part float up and down smoothly with math?

So I know you can use like big numbers like 1/20 to get .05, and keep upgrading that number to slow it down more as it goes up. But the sink/float parts to using just this method don’t look smooth. What can I do?

Here’s a little snippet of code I was trying to do to make the part float up and down smoothly:

if float and upNum + 20 >= 500 then
				sink=true
				float=false
				print'sink time'
			else
				if float then
					upNum = upNum + 20
				end
			end
			if sink and upNum - 20 <= -500 then
				float=true
				sink=false
				print'up time'
			else
				if sink then
					upNum = upNum - 20
				end
			end
			Paw.CFrame = (Paw.CFrame + Paw.CFrame.LookVector * .5) -- - Vector3.new(0,.01/upNum,0)
			if upNum > 0 then
				if float then
					Paw.CFrame = Paw.CFrame + Vector3.new(0,10/upNum,0)
				elseif sink then
					Paw.CFrame = Paw.CFrame - Vector3.new(0,10/upNum,0)
				end
			end
end
1 Like

Ok do you want it to move up and down infinitely WITHOUT stopping? if so you can use this

local repeatamount = 20 -- change till it moves as much as you want. (Can only be whole numbers)
local waittime = 5 -- change to time to wait before moving again
local brick = script.Parent -- You can change this to the brick to move

while true do
for i = 1, repeatamount do
brick . Cframe  = brick.CFrame + brick.CFrame.UpVector * .1
wait()
end
wait(waittime)
for i = 1, repeatamount do
brick . Cframe  = brick.CFrame + brick.CFrame.UpVector * -.1
wait()
end
wait(waittime)
end
3 Likes

This really wouldn’t look very smooth. I would recommend using the math.sin or math.cos methods to determine the upwards/downwards offset of the part.

This would look something like this:

local amplitude = 3 -- offset on both sides
local magnitude = 2.5 -- time it takes to move from the bottom -> top, and top -> bottom

local part = workspace:WaitForChild("Part")
local defaultCFrame = part.CFrame

game:GetService("RunService").Stepped:Connect(function()
	part.CFrame = defaultCFrame + Vector3.new(0, amplitude*math.sin(tick()*math.pi/magnitude), 0)
end)

Basically what this does is it calls sin(x) where x = tick() * math.pi / magnitude.

We set x equal to this because it gets the current time, multiplies it by pi to make the code iterate such that 1 second = the movement from the middle to the top, and then back down to the middle, which effectively makes the movement from the bottom to the top, and the top to the bottom both 1 seconds each. It then divides it by the magnitude so that the amount of time it takes to get from the bottom to the top is equal to the magnitude.

18 Likes