Creating gravity around an object based on mass in Unity

Ever wanted to make a space game? The biggest challenge is creating the gravity for your object in space. They need to exert different gravity forces based on their mass. You can bend physics laws a little but if you need the game to look realistic, you must apply the physics in your script. In this tutorial, we will create a gravity script for planets that changes the gravity force based on the mass of the planet.

Let’s get started

Physics behind gravity

Gravity created by an object is directly proportional to its mass. So, the bigger the object larger the force of gravity. Gravity is also inversely proportional to the distance between the objects. The farther away the object the less force it experiences.

Keeping these properties in mind and bending the rules a little let’s make our gravity script.

Things to take care of in your scene

  1. Disable the inbuild gravity in Unity by going to Edit>Project Settings>Physics. Set the gravity to zero in all axis.
  2. Add Rigidbody component to all your game object that will react to gravity or will apply gravitational force on other objects.
  3. Disable any other force acting in your scene temporarily.

The gravity script

Here is the whole script. If you don’t want to understand the script, you can just copy and paste the code below and increase the mass of the object to increase gravity.

Remember to increase the drag as per the mass or the object will start drifting.

using UnityEngine;

public class Gforce : MonoBehaviour {

	float radius;
	Rigidbody rg,rb;
	
	
	void Start()
	{


                rg = GetComponent<Rigidbody> ();
		radius = rg.mass*6 / 10;
		
		

	}

	void FixedUpdate()
	{
		
		Collider[] Planets = Physics.OverlapSphere(transform.position, radius);

		foreach(Collider hit in Planets)
		{
			if (hit.gameObject != this.gameObject) {				
				Vector3 distanceV = this.transform.position - hit.gameObject.transform.position;
				float distance = distanceV.magnitude;
				rb = hit.gameObject.GetComponent<Rigidbody> ();
				Vector3 forcee = (rg.mass + rb.mass / Mathf.Pow (distance, 2)) * distanceV.normalized;
				rb.AddForce (forcee);
			}

		}
	}

}

Let’s break down the code

First define a float that tells us the radius of the sphere within which gravity will apply.

Then we need two rigidbody variables, one for this gameobject and the other for the object on which it will apply gravity. These will be rg and rb.

We will use FixedUpdate for this script as all our functions are physics based.

In the Fixed Update we will create a sphere of radius 0.6 times the mass of the object. 0.6 is just my value for radius. You can change it to your requirement.

We will collect all the colliders inside the sphere into an array. Then get the rigidbody attached to those colliders and apply force based on the mass and distance of the objects.

Bigger object applying gravity on smaller object

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from VionixStudio

Subscribe now to keep reading and get access to the full archive.

Continue reading