3D Planatary Gravity

I am using RigidBodies and want my planets to use real physics bases off of their mass and want to have that gravity to interact with other RigidBodies in the system.

Step 1: Turn off gravity

Step 2: Create some kind of ‘forcefield’ script on your planets. There are a lot of ways of doing this, but the simplest one would be to use Physics.OverlapSphere and then apply forces to everything in that area. Here’s a sample:

// Make sure to add the line 'using System.Collections.Generic'
// on the first line of the file

public float forceRadius = 20;
public float gravPower = 9.81f;
void FixedUpdate()
{
    // Populate a list of nearby bodies
    List<Rigidbody> bodies = new List<Rigidbody>();
    foreach(Collider col in Physics.OverlapSphere 
        (transform.position, forceRadius))
    {
        if(col.attachedRigidbody != null && !bodies.Contains(col.attachedRigidbody))
        {
            bodies.Add(col.attachedRigidbody);
        }
    }
    // Now you have your rigidbodies, time to add the force!
    foreach(Rigidbody body in bodies)
    {
        float bodyDist = (body.position - transform.position).sqrMagnitude;
        float gravStrengthFactor = Mathf.Pow(forceRadius) / bodyDist;
        body.AddForce(gravStrengthFactor * gravPower * 
           (transform.position - body.position) * Time.deltaTime, ForceMode.Acceleration);
    }
}

Of course, there are a whole slew of optimisations you can apply to this, but it’s a start.

Unity’s physics engine only simulates gravity from one gravitational body. The RigidBody objects have a force applied to them in the down (-ve Y) axis only. There is no concept of creating additional gravity fields/forces that would act as attractors to other objects.