- Home /
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.
Answer by skovacs1 · Oct 01, 2010 at 09:36 PM
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:
- Have a global script that tracks and applies all gravity calculations,
- 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[i];
for(var j : int = i + 1; j < objects.length; j++) {
var object2 : Rigidbody = objects[2];
var force : Vector3 = calculateGravity(object1, object2);
object1.AddForce(force);
object2.AddForce(-force);
}
}
whereas the per-body script would need to iterate through everything that could act upon it:
//O(n^2) because this is done for every object and it performs on every other object
for(var other: Rigidbody in otherObjects)
transform.AddForce(calculateGravity(other));
It's really not that much to keep track of if it's just gravity.
I figured there wouldn't be any easy solution, but it doesn't hurt to ask. :P
I think I'll simply set the planet to apply a gravitational force to any players or objects within the Trigger area. That'll be a pain getting the character stable through it all, but it'll be worth it to create such a universal system.