- Home /
[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 () 
 {    
     rigidbody.AddForce(Direction.TransformDirection(Vector3.forward * Forward_Speed * Time.deltaTime));
     
     transform.RotateAround(Planet.transform.position, Vector3.up,Speed * Time.deltaTime);
 }
Thank you in advance and sorry for my bad english :)
if you have reedit your question to [SOLVED] why don't you accept the answer that was helpful to you?
if you do not know how watch this: Unity Answers Tutorial
Answer by lktfl · Apr 08, 2013 at 05:12 AM
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;  
2) create an if statement to check if your attracted object is inside your attracting object's gravitational radius:
 var gravitationalRadius : int;
 
 function Update(){
    //Do stuff
    if ( distance <= gravitationalRadius){
 
       // Do gravitational physics stuff here
 
    }
 
 }
Answer by HunterKrech · Apr 08, 2013 at 12:37 AM
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;
         addingForce = false;    
     }
 } 
Make sure that you attach a large sphere collider to the planet, that is triggered, and put this code on the ateroids.
Answer by arjunxxxl · Jun 21, 2018 at 04:07 AM
I have made video to simulate realistic asteroid gravity. Take a look here : Gravity Simulation
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                