# Unity Physics - Attract(Rigidbody body)

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.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?

• If the player is between two planets, should they a) fall slowly toward one of them (because the other planet is pulling as well) or b) fall "normally" toward the nearest? – Victor T. Apr 22 '17 at 16:14
• @VictorT. preferably toward toward the one with the highest planetGravity – TheGejr Apr 22 '17 at 16:23
• That doesn't seem like it matches your original post, where you specified that the closest planet should attract the player. Maybe the distance calculation should be weighted by gravity? In either case, it seems reasonable to start with a simple loop over all the planet objects to find a "best match" for the player. – Victor T. Apr 22 '17 at 17:07
• @VictorT. I have clarified my question i hope that helps understanding what the optimal result would be. But thanks any way! – TheGejr Apr 22 '17 at 17:22

F=a*m/d

a is a constant to control the gravity.
m is the mass/size of the planet.
d is the distance from player to planet.

if you apply this for every planet then it should be pretty realistic. maybe you can ignore a planet when the distance is greater than b*m where b is some other constant to control the size

• Great idea! Do you have any suggestion on how to implement this? I will try but im not the greatest at physics :P – TheGejr Apr 22 '17 at 17:52
• You'd need to have a list of planets and then go through them in the player. then calculate the distance with (planetpos - playerpos).magnitude put that into the formula and add the force to the player. Hope that helps :) – TimoHo Apr 22 '17 at 17:57
• Nice! i got it to work! Thanks for the help, my planets now acts like "true" planet, where the longere away you move the less gravity is there Thanks! – TheGejr Apr 22 '17 at 18:38
• Good to read that I actually helped 😁 – TimoHo Apr 22 '17 at 18:40

I have recently written about this issue. I will say it here again so that it may be of use:

Use the existing Physics2D. Don't reinvent the wheel. There's a component called PointEffector2D, you're trying to achieve its effects manually, which will end badly if it's not implemented correctly.

Here's the manual for effectors. What you want, is to have inverse squared force mode with a negative number as the force. You don't need a rigidbody on the planet but you do need a Collider2D that will be used by effector.