I created a script cs, which adds to the planet’s gravitation. Then the planet attracts rigidbodys, but the distance is always the nearest rigidbody and not for each other, so if one rigidbody is closer to the planet and is quickly attracted to other rigidbodies being far away from it are attracted just as quickly as the next.
I put the script below:
That script is so short, why attach it like that? Just post it inline. Remember always use code tags:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Gravity2 : MonoBehaviour {
public List<Rigidbody> objects;
public GameObject plant;
public float mass = 0;
public float force = 0;
public float distance = 1000;
void Update () {
force = (mass * mass) / (distance * distance);
}
void FixedUpdate()
{
foreach (Rigidbody o in UnityEngine.Rigidbody.FindObjectsOfType<Rigidbody>())
{
if(o.GetComponent<Rigidbody>() && o != plant)
{
distance = Vector3.Distance(this.transform.position, o.transform.position);
o.AddForce((plant.transform.position - o.transform.position).normalized * force);
}
}
}
}
Also, you’re calculating the force in Update, but setting distance during the loop of FixedUpdate… whenever Update gets called, distance will equal the distance of the last Rigidbody found during that loop.
Also… you call FindObjectsOfType, the variable ‘o’ is a Rigidbody… why are you then calling ‘o.GetComponent()’? It’s a Rigidbody, I’m fairly certain it has a Rigidbody!
Furthermore you say ‘o != plant’, but plant (is that supposed to be planet???) is a GameObject. ‘o’ is a Rigidbody, and ‘plant’ is a GameObject. They’ll NEVER be equal.