How do I make an object orbit around another object?

I want it so that when the player inputs ScrollUp or ScrollDown, the first object rotates or orbits around another object by 22.5 degrees or -22.5 degrees in Y-axis. How do I accomplish this?

1 Like

Hello there!

I can’t test the scripts right now, so it might not work, but it’ll be something like that

We can use the math.cos and math.sin to make a circular path

I’ll use planet as the base object and moon as the rotating one

local Value = 0

local Distance = 10

local function Rotate(Action, Input, State)

Moon.CFrame = Planet.CFrame + (CFrame.new(math.cos(Value), math.sin(Value), 0) * Distance)

end

Here you’ll need to make a script to chance the value with the mouse wheel

Can’t help with it now sry

This or you could just weld the ‘Moon’ to a phantom part inside the ‘planet’ and rotate the phantom block.

Think it might work better

To detect the mouse wheel you can use ContextActionService or InputService

1 Like

No. I tried the weld before, it doesn’t work for me at all.
The center object does rotate, but not the orbiting one.

1 Like

Have you tried his code? It seems promising and accurate

1 Like

I forgot to mention it in the first reply. Yeah, I will be testing his code out soon

1 Like

this is a simple script just wrote to show how to rotate fully around an object

local MainPart = script.Parent
local OriginalCFrame = script.Parent.CFrame
local CurrentRotation = 0  -- current rotation
local RotationRate = 2
local Distance = 10  -- distance out from mainpart

while true do
	CurrentRotation += RotationRate
	-- Need to get the base cframe with the new rotation
	local NewCFrame = OriginalCFrame * CFrame.Angles(0, math.rad(CurrentRotation), 0)
	-- now get the direction infront of this rotated cframe to place the part that rotates around the mainpart
	workspace.PartToRotate.CFrame = CFrame.new(OriginalCFrame.Position + NewCFrame.LookVector * Distance)
	task.wait()
end

and here is one set with limits of degrees

local MainPart = script.Parent
local OriginalCFrame = script.Parent.CFrame

local AddRotation = true

local MinRotation = -22
local MaxRotation = 22

local CurrentRotation = MinRotation  -- current rotation   -- set to start rotation
local RotationRate = 1  -- added each loop
local Distance = 10  -- distance out from mainpart

while true do

	-- Need to get the base cframe with the new rotation
	local NewCFrame = OriginalCFrame * CFrame.Angles(0, math.rad(CurrentRotation), 0)
	-- now get the direction infront of this rotated cframe to place the part that rotates around the mainpart
	workspace.PartToRotate.CFrame = CFrame.new(OriginalCFrame.Position + NewCFrame.LookVector * Distance)
	if AddRotation then 
		CurrentRotation += RotationRate
	else
		CurrentRotation -= RotationRate
	end
	if CurrentRotation >= MaxRotation then  -- need to add to get to max degrees
		AddRotation = false
	elseif CurrentRotation <= MinRotation then -- need to add back to stay inbetween degrees
		AddRotation = true
	end
	task.wait()
end

Commented these to help understand how it works

1 Like

No luck with this. Here’s how it looks

Edit: Wait, I’m stupid. I used FindFirstChild instead of WaitForChild. Fixing it
Edit 2: Error says “invalid argument (Vector3 expected, got number)”

local value = 0
local center = game.Workspace:FindFirstChild("Center")
local orbiter = game.Workspace:FindFirstChild("Orbiter")
local Distance = 10

local function spin()
	orbiter.CFrame = center.CFrame + (CFrame.new(math.cos(value),math.sin(value),0)*Distance)
end

while true do
	task.wait(0.01)
	spin()
end

Try this

local Center = workspace.Center
local orbiter = workspace.Orbitor

local Angle = 0
local Radius = 3 -- Distance away from Center's center 
local Increment = 22.5

while task.wait(1) do
	Angle += Increment
	
	local Position = Vector3.new(math.cos(math.rad(Angle)) * math.abs(Radius), 0, math.sin(math.rad(Angle)) * math.abs(Radius))
	orbiter.CFrame = Center.CFrame * CFrame.new(Position)
end

1 Like

This worked, thank you. I will go with this

Actually nvm, this is better. Thank you for the help

I dont think I need to say this but I’ll say it anyway… The angle is in degrees so basically 0 to 360 degrees. Your degrees can also be negative and they can be above 360 btw so don’t worry about it

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