# HOW TO ADD GRAVITY TO A OBJECT?

Hi everyone I wanted to add gravity to a object but not the way it is usually done.
I want to add gravity to my object A that affects object B like stars and planets, where the planets are pulled by the gravitational force of the star.So if object B is too close to object A while moving it will collide with it, but if it is at a proper distance then it will be affected by the gravity but it will not collide , only change its direction.THE IMAGE BELOW WILL MAKE IT EASY TO UNDERSTAND WHAT I WANT.
NOTE It is a 2d game.
thank you

Implement Newton’s law of universal gravitation. Wikipedia has a lot of jargon, You can probably find a simpler explanation somewhere else:

Force (F) of gravitational attraction between two objects with Mass1 and Mass2 at distance D is:

`````` F = G(mass1*mass2)/D squared.

(G is the gravitational constant, which has the same value throughout our universe.)
``````

So basically, create a script for your objects that have a Mass and a Velocity. And have another script that will use the formula to calculate the relative forces, you just then update the object’s velocity based on the calculated force and direction.

Edit —

If you’re looking for a simpler solution, just for the example that you’ve provvided. You can probably just have 1 object move towards a point and if the objects get too close, then you change the target that the object moves towards. Psuedo example:

`````` if (Vector3.Distance(star.transform.position, planet.transform.position) < 1f)
{
// objects are within proximity
targetPosition = star.transform.position; // set targetPosition to the star
}
else
{
// objects are not close enough to each other
targetPosition = someOtherPosition; // set targetPosition to some reference
}

planet.transform.position = Vector3.MoveTowards(planet.transform.position, targetPosition);
``````

well, this kinda works, maybe you can tweak it. (its for 3d sorry but the principle is the same)

``````using UnityEngine;
using System.Collections;

public class gravityTest : MonoBehaviour {

// Use this for initialization
public float influence;
public float mass;
public bool isPlayable = false;
float speed = 1; //not sure how you will find this out, it may be easier in 2d?
Rigidbody body;

void Start () {
body = gameObject.GetComponent<Rigidbody> ();
}

// Update is called once per frame
void Update () {
if (isPlayable) { //this is for testing purposes however you control the object is up to you.
if (Input.GetKeyDown (KeyCode.A)) {
body.AddForce (new Vector3 (0, 10f, 0));
}
}

Collider[] hitColliders = Physics.OverlapSphere(transform.position, influence);
int i = 0;
while (i < hitColliders.Length) {
Debug.Log(hitColliders*.name);*
``````

_ if(hitColliders*.tag == “planet”)_
_
{_
_
gravityTest test;_
_ test = hitColliders.gameObject.GetComponent();
if(test.mass > mass)
{
body.AddForce((hitColliders.transform.position - transform.position) * speed * Time.smoothDeltaTime);
}
}
i++;
}*_

* }*
}
this simply attracts an object to another, the one with the smaller mass being attracted to the larger one, you have to tag the objects “planet”. see how you get on with it