# [Solved]How to make a planet gravity that affect asteroid ?

Hi, as you can read in the title i want to know how to make a planet gravity that affect the asteroids with rigidbody, and if it hit something big the asteroid will get out of the planete orbit.

that is what i did:

``````var Forward_Speed : float = 0.2;
var Direction : Transform;

var Planet : Transform;
var Speed : float = 0.8;

function Start() {
Gravity = 0.00;
}
function LateUpdate ()
{

transform.RotateAround(Planet.transform.position, Vector3.up,Speed * Time.deltaTime);
}
``````

Have you checked the planetary gravity script on UnityWiki?
Here

It will give you a starting place for your gravity script.

I wouldn’t do important physics stuff like gravity inside of `LateUpdate()`. You should do those kinds of calculations in `Update()` or `FixedUpdate()`.

Also, if you want to have something like have a gravitational radius you just need to:

1. get the distance between attracting object and the attracted object. Do this by saying:

var distance = Vector3.Distance(attractingObject.transform.position, attractedObject.transform.position);

or

``````var distance = attractingObject.transform.position - attractedObject.transform.position;
``````
1. create an if statement to check if your attracted object is inside your attracting object’s gravitational radius:

function Update(){

``````//Do stuff

// Do gravitational physics stuff here

}
``````

}

Did this Same thing for my game here .

``````void OnTriggerStay(Collider Me)
{
GravDir = PlanetPos.transform.position - transform.position;
if(Me.gameObject.tag == "GravityField")
{
rigidbody.useGravity = false; gravity, or whatever
transform.position = GravDir;
}

}
void OnTriggerExit(Collider Me)
{
if(Me.gameObject.tag == "GravityField")
{
rigidbody.useGravity = true;// depending if you want to re-apply
rigidbody.constantForce.force = Vector3.zero;