Hello,
I’m trying to make a 2D simulation game of planets with gravity that will let you place planets. I’ve made a script that will add gravity to every object in the scene but it requires the main sun to orbit, Basically i’m trying to make a script that will add gravity to every object based on its mass and not have to have on object be the main orbit so everything can move and big objects will influence most over little objects. My script right now is okay but its not what i’m trying to get and I just have no idea what to do now.
using UnityEngine;
public class PlanetGravity : MonoBehaviour {
public float massmul;
public GameObject sun;
void FixedUpdate () {
foreach (GameObject o in UnityEngine.Object.FindObjectsOfType<GameObject>())
{
Rigidbody2D RBody = o.GetComponent<Rigidbody2D>();
if (RBody && o != sun)
{
RBody.AddForce((sun.transform.position - o.transform.position).normalized * (RBody.mass * massmul) / Vector3.Distance(sun.transform.position, o.transform.position));
}
}
}
}
If anyone could help ill appreciate it!