# adapting a Unity gravitational script to allow moons

I'm using this script: http://wiki.unity3d.com/index.php/Simple_planetary_orbits

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.

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.

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.

• 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