Gravitational Object Creation for 3D Game

I want to create a gravitational object which attracts other objects when they come within its range from all direction.

What kind of code, I require to write for this purpose?

You need to use the gravitational force formula or an equivalent and then to compute the total force applied on your object. The simple approach would be to use Unity Physics with rigidbody attached to yours objects. Something like this (not tested) :

    public Rigidbody[] rigidbodies;
    Rigidbody rigidbody;

    const float G = 6.67408e-11f;

    void Start()
        rigidbody = GetComponent<Rigidbody>();

    void FixedUpdate()
        Vector3 totalForce =;
        foreach (Rigidbody rb in rigidbodies)
            float r = Vector3.Distance(rigidbody.centerOfMass, rb.centerOfMass);
            Vector3 direction = rb.centerOfMass - rigidbody.centerOfMass;
            float force = rigidbody.mass * rb.mass * G / (r * r);
            totalForce += direction * force;

alt text