Gravity and the Physics Manager.

So I'm working on a planetary gravity system, and I can do it really well through script if I want.

But I wonder if there might be a more universal shortcut. I notice a "Gravity" vector in the Physics Manager. You think there might be some way to make gravity separate for separate objects, still using the "Use Gravity" variable?

Otherwise, I'll have to make all my objects share a universal gravity script, but I find that to be a little hard to keep track of for eeeeeverything.

On that note, I'm using Unity 2.6.1.

I'm fairly certain it's an all or nothing sort of thing. If Rigidbody.useGravity=true, the Rigidbody will be affected by the Physics.gravity Vector3.

If you were to try changing Physics.gravity at run-time, you would likely get un-predictable results depending on when things are applied and you would still need to iterate through each of the other gravitational bodies just to calculate the gravity acting on the current body anyways because gravity is a direction, not a point in space.

You would really need to either:

  1. Have a global script that tracks and applies all gravity calculations,
  2. or have a script on each of the gravity-affected bodies which applies calculations to that body

The global script could be optimized to do fewer calculations through the use of jagged loop iteration:

//isn't this like O(nlogn)? It's at least better than O(n^2)
for(var i : int =  0; i < objects.length-1; i++) {
    var object1 : Rigidbody = objects*;*
 *for(var j : int = i + 1; j < objects.length; j++) {*
 *var object2 : Rigidbody = objects[2];*
 *var force : Vector3 = calculateGravity(object1, object2);*
*<p>whereas the per-body script would need to iterate through everything that could act upon it:</p>*
*//O(n^2) because this is done for every object and it performs on every other object*
*for(var other: Rigidbody in otherObjects)*
*<p>It's really not <em>that much</em> to keep track of if it's just gravity.</p>*