Creating Virtual Planets From Virtual Matter 🌐

Nicolas Gatien
5 min readJun 26, 2021

After building my first 3D game a few weeks ago, I decided to step it up a notch and simulate the entire universe…

Alright maybe take it down a few notches. Maybe just simulating the creation of planets would work. So heres a step by step guide to simulating planets! :D

Step 1: Create Matter

Go to your Hierarchy in the top left corner of your screen and create a Sphere by right clicking > 3D Object > Sphere.

Rename the Sphere to “Matter” and add a Rigidbody component to it.

Uncheck “Use Gravity” and add a new script called “Gravity”.

Open Gravity, copy and paste this script into it.

using UnityEngine;[RequireComponent(typeof(SphereCollider))]
public class Gravity : MonoBehaviour
{
public float gravityPull;
public static float m_GravityRadius = 1f;
void Awake()
{
m_GravityRadius = GetComponent<SphereCollider>().radius;
// set gravity radius to SphereCollider radius
}
void OnTriggerStay(Collider other)
{
if (other.attachedRigidbody)
{
float gravityIntensity = Vector3.Distance(transform.position, other.transform.position) / m_GravityRadius;
other.attachedRigidbody.AddForce((transform.position — other.transform.position) * gravityIntensity * other.attachedRigidbody.mass * gravityPull * Time.smoothDeltaTime);
Debug.DrawRay(other.transform.position, transform.position — other.transform.position);
}
}

Open the Sphere Collider, check “Is Trigger” and set the “Radius” to 10.

Add a new script called “ApplyNumbers”.

Open ApplyNumbers, copy and paste this script into it.

using UnityEngine;public class ApplyNumbers : MonoBehaviour
{
Rigidbody rb;
Gravity g;
void Start()
{
rb = GetComponent<Rigidbody>();
g = GetComponent<Gravity>();
}
void Update()
{
rb.mass = transform.localScale.x * 4 * transform.localScale.x;
g.gravityPull = transform.localScale.x * 2;
}
}

Create a new tag named “Matter” and add it to the Matter GameObject.

Step 2: Add Collision

Create a sphere child object of the Matter gameobject.

Remove the Sphere (Mesh Filter) and Mesh Renderer components from the Sphere gameobject.

Add a new script called “Matter”.

Open Matter, copy and paste this script into it.

using UnityEngine;public class Matter : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
if(other.transform.parent != null)
{
if (other.transform.parent.transform.localScale.x > transform.parent.transform.localScale.x)
{
other.transform.parent.transform.localScale += new Vector3(transform.parent.transform.localScale.x / 4, transform.parent.transform.localScale.y / 4, transform.parent.transform.localScale.z / 4);
Destroy(transform.parent.gameObject);
}
else
{
transform.parent.transform.localScale += new Vector3(other.transform.parent.transform.localScale.x / 4, other.transform.parent.transform.localScale.y / 4, other.transform.parent.transform.localScale.z / 4);
Destroy(other.transform.parent.gameObject);
}
}
}
}

Open the Sphere Collider, and check “Is Trigger”.

Drag the Matter gameobject into the Assets window to create a prefab.

Delete the Matter gameobject from the scene view.

Step 3: Spawning Matter

Create an empty gameobject and name it “Spawner”.

Add a new script called “Spawner”.

Open Spawner, copy and paste this script into it.

using UnityEngine;public class Spawner : MonoBehaviour
{
public int numOfObjects = 25;
public GameObject planet;
public float spawnRange = 25;
public float maxPlanetSize = 2f;
public float minPlanetSize = 0.3f;
void Start()
{
while (numOfObjects > 0)
{
Vector3 planetPos = new Vector3(Random.Range(-spawnRange, spawnRange), Random.Range(-spawnRange, spawnRange), Random.Range(-spawnRange, spawnRange));
Quaternion planetRotation = new Quaternion(Random.Range(0, 365), Random.Range(0, 365), Random.Range(0, 365), 1); GameObject curPlanet = Instantiate(planet, planetPos, planetRotation); curPlanet.GetComponent<Rigidbody>().velocity = new Vector3(Random.Range(-10f, 10f), Random.Range(-10f, 10f), Random.Range(-10f, 10f)); float fsize = Random.Range(minPlanetSize, maxPlanetSize); curPlanet.transform.localScale = new Vector3(fsize, fsize, fsize); numOfObjects--;
}
}
}

Drag the Matter gameobject from the Assets view into the “Planet” space in the Spawner script.

Extra Resources:

And with that if you press play, the simulation will run!

In the original project I also have a camera following script to lock on to Matter and see where it’s going, but I followed this tutorial to put that part of the project together.

I also have the VR component added, so that you can watch the simulation run in VR. I used the same technique that I showed in this article to import VR function into my project.

And finally if you want to see all these scripts, you can check the Github repository here!

Hope this article helped you get started, if you have any questions post them in the comments! See you soon! 👋

--

--

Nicolas Gatien

Hi! I'm Nicolas! A 17-year-old game designer & maker. I build things and write about them!