Levitating Part

I am trying to make a part that moves up and down while rotating
Problem: The part keeps changing its initialPosition causing the part to not work right.

Help: When I us this code it works grate, but I have to put this into each part and change the initialPosition to the part’s position

local part = script.Parent
local angle = 0
local speed = 1
local amplitude = 1
local initialPosition = Vector3.new(35, 508.25, 35)

part.Position = initialPosition

while true do
	angle = angle + speed
	part.Orientation = Vector3.new(0, angle, 0)

	local heightOffset = math.sin(math.rad(angle)) * amplitude
	part.Position = initialPosition + Vector3.new(0, heightOffset, 0)

	wait()
end

Code:

local folder = workspace:WaitForChild("PowerUps") 

local speed = 1
local amplitude = 1

while true do
	for _, part in pairs(folder:GetChildren()) do
		if part:IsA("BasePart") then
			local initialPosition = part.Position
			local angle = tick() * speed  -- Using tick() instead of a global angle variable
			local heightOffset = math.sin(angle) * amplitude
			part.Position = initialPosition + Vector3.new(0, heightOffset, 0)
		end
	end

	wait()
end

Hi,

I rewrote your script a bit, but in doing so the speed and amplitude of the sine wave no longer matches the original. You can reconfigure the variables to match the previous outcome.

Revised code:

local runService = game:GetService("RunService")

local folder = workspace:WaitForChild("PowerUps") 

local speed = 1
local amplitude = 1

local origins = {}

while true do
	for _, part in pairs(folder:GetChildren()) do
		if part:IsA("BasePart") then
			origins[part] = origins[part] or part.Position
			local origin = origins[part]
			local angle = time() * speed  -- Changed to time() for better accuracy
			local heightOffset = math.sin(angle) * amplitude
			part.Position = origin + Vector3.new(0, heightOffset, 0)
		end
	end
	
	runService.Heartbeat:Wait()
end

Try this:

local folder = workspace:WaitForChild("PowerUps") 

local speed = 1
local amplitude = 1
local angle = 0

while true do
	for _, part in pairs(folder:GetChildren()) do
		if part:IsA("BasePart") then
			part.Orientation = Vector3.new(0, angle, 0)
			local yOffset = amplitude * math.sin(speed * tick())
			part.Position = Vector3.new(part.Position.X, yOffset, part.Position.Z)
		end
	end
	angle = angle + speed
	wait()
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.