Simple Planet orbit


Can I get a simple script for movement of an planet in an orbit. I tried quite a few scripts, but as I am not an very gud programmer I had some trouble dealing with the errors.

Please provide me with an simple orbit script for a planet revolving around an Star.

Avichal Singh


I'm not sure why I'm writing a script for you, but just follow these steps:

  • Make an empty gameObject. Name it "CenterOfGravity" (or the equivalent in your language)
  • Make the planet(s) you want to rotate a child of the CenterOfGravity gameObject.
  • Copy the script at the bottom of this answer onto your CenterOfGravity gameObject.
  • For realistic behavior, move the CenterOfGravity gameObject to the position of your star.

JS script to put on your CenterOfGravity gameObject:

var rotationSpeed = 5;

function Update() {
    transform.Rotate(0,rotationSpeed * Time.deltaTime,0);

Or in C#:

float rotationSpeed = 5.0f;

void Update() {

Please note that the above scripts are untested and may contain errors

Now, asking for a script and actually getting somebody to write it for you isn't very common and overall isn't a very good idea, so please try to avoid this kind of question in the future. Instead, a better way to do it might be to actually start trying to write the script yourself, and then asking somebody here if you have a problem with it.

Also, if this answer works for you, please accept it as correct. :)

You could also just use the Animation editor to do what Elliot says. Make the planet a child (of the star or hidden object at the star) and animate that parent's rotation.

How simple is simple? If you want the planet to rotate as well as revolve around the planet, in a perfectly circular orbit, but its axis tilted (as most are), in addition to what Elliot has said, you'd want to have the planet rotate around itself too. But that's not enough, because the axis of rotation is constant relative to the universe, not just itself. Meaning if you did simply that, the planet's axis would always point away from the star. So you'd have to make the planet a child of yet another gameobject (empty or otherwise invisible), and that gameobject would counter-rotate at the same speed as the revolution around the star. Or maybe script it to set world-rotation (universe rotation in this case) at a constant axis. If you want orbits not on Y axis or elliptical, or based ephemeris, ping me directly.

@Avichal just use this javascript on any object (empty one works the best)

function Update () {
    transform.Rotate(0, 1, 0, Space.World);

then add the planet as the child