I have this script Gravity.cs
public class Gravity : MonoBehaviour
{
public float planetGravity = -10f;
public void Attract(Rigidbody body)
{
Vector2 normalForce = (body.position - transform.position).normalized;
Vector2 localUp = body.transform.up;
body.AddForce(normalForce * planetGravity);
body.rotation = Quaternion.FromToRotation(localUp, normalForce) * body.rotation;
}
}
In this script I make a "gravity field", for a 2D sprite of a planet, I then have my player walk around it.
My problem, I want multiple of these planets, all with different gravity fields, When the player is out of the sphere of influence (a trigger sphere collider in unity) the player should be attracted to the closest planet with the highest gravity eg. if the player is exactly between two planets the player should fall to the planet with the highest gravity(or mass idk), how can I do this?