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

I am trying to make a Moon revolve around a planet. How do i do that?

1 answer

All that is required here is some geometrical description of the movement of the moon, and an update loop/coroutine to process it.

You could describe the orbit of a planet around another, particularly in computer simulation, in various ways depending on what you intend to do with it.

I doubt you'll want to simulate the actual gravitational relationship of bodily orbits, but rather just the approximate circular movement of your moon around a planet.

So what you'll need in the script is: -A reference to to the center of rotation, the three dimensional coordinate that you want the want the moon to rotate around

-A reference to the moon object (a Part)

-A vector (Vector3 in Roblox) describing the direction of the axis about which the moon rotates: For example, if you want the moon to rotate around the planet horizontally, you need to rotate around the y-axis, which is (0,1,0) in vector form.

-The radius of orbit, i.e. how far away you want the moon to be from the planet

The following script will work if:

A) There is a Part called Orbit in the Workspace

B) There is a Part called Center in the Workspace

center = workspace.Center.Position --Coordinate Reference

body = workspace.Body --Reference to the 'moon'

axis = Vector3.new(0.25,1,0.5) --Change these numbers to change axis of rotation

radius = (body.Position - center).Magnitude --Calculates distance based on where you leave the moon in the workspace

space = CFrame.new(center,center+axis) --This is a CFrame for the coordinate space we've made

--Variables to keep track of speed and time
delta = tick()
timer = 0
multiplier = 1 --If you want it to go twice as fast, use 2, or half as fast, use 0.5, etc...

--A function to update the position of the moon
game:GetService("RunService").RenderStepped:connect(function()
	--Triggers every ?1/60th of a second
	delta = tick() - delta
	timer = timer + delta

	body.Position = space:toWorldSpace(CFrame.new(radius*math.cos(timer*multiplier),radius*math.sin(timer*multiplier),0)).p

	delta = tick()
end)

Hope that helps!

1 comment

0

Hmmmm... Dind't seem to work.

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