Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using this script:

to get a solar system going in Unity, but it doesn't seem to support creating bodies that orbit other moving bodies (or I am using it incorrectly).

Any idea about how to modify it so that it does (or just use it correctly)? I've been beating my head against this problem for a couple hours, and I really don't feel like I have any idea what I'm doing.

Thanks in advance.

share|improve this question
You should check out the 3 body problem for the history of this problem and why it often has to be faked in games. – DampeS8N Nov 19 '12 at 19:55

By the look of things you should be able to apply this script to your moon and simply point to the planetary object you want it to orbit. However given the fact that your planet is moving, it looks like your moons will probably be playing catch up to the planets orbiting the sun. You should be able to fix this with a simple hierarchy change.

 - Sun Container Object (Parent)
   - Sun Model          (Child)
   - Planet Container Object 1
     - Planet Model 1
     - Moon Container Object 1
       - Moon Model
     - Moon Container Object 2 (if your planet has two or more moons)
       - Moon Model
   - Planet Container Object 2
     - Planet Model 2

The important thing to notice is that by parenting your moon objects to your planet objects they will gain the benefit of the planets orbit for free. Then they just have to adjust themselves for their localized planetary orbit. Also be sure to make your models children of empty container objects, this way you can have them rotate without messing up the orbits of the their child celestial objects.

share|improve this answer

It seems that the property

var orbitAround : Transform;

is a stationary object. You'll need to modify the code to allow this to be updated, so as your planet moves around the sun, it's moons have to revolve around the center of the planet.

share|improve this answer
Tranforms make up the scene graph/object hierarchy in Unity, so this value shouldn't need to change - it just needs to point to the relevant object. So, a Moon object would have orbitAround=Earth, and the Earth object would have orbitAround=Sun, etc. – Kylotan Nov 19 '12 at 16:51

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.