Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by chuck16 · Feb 14, 2019 at 11:57 AM · 3dorbitrocketfauxgravity

Rocket game orbit problems *URGENT*

I'm making a rocket game. I have made faux gravity and the gravity works fine. So I went on to make gravity and drag get less as you go up. Theoretically, after you have 0 drag and a little bit of gravity you should orbit. But the rocket did not orbit?!!?. Please tell me how to make the rocket orbit. :)

Here is the Code for gravity and such:

 public Transform _Planet;
     public float Gravity;
     public Rigidbody rocket;
     public Transform rocketT;
     public float DistanceEarth_;
     public float atmosHeight;
     public bool oneOver = true;
     public bool twoOver = true;
     public bool threeOver = true;
     public bool fourOver = true;
     public bool fiveOver = true;
     public bool SphereOF_influ_out = true;
 
     // Update is called once per frame
     void Update()
     {
         DistanceEarth_ = Vector3.Distance(rocketT.position, _Planet.position) - 246601.54f;
         rocket.AddForce((_Planet.position - transform.position).normalized * Gravity);
         if (DistanceEarth_ < atmosHeight / 5)
         {
             Gravity = 30;
             rocket.drag = 2;
 
         }
           if (DistanceEarth_ > atmosHeight / 5 &&DistanceEarth_ < (atmosHeight / 5) * 2  )
            {
                Gravity -= Gravity / 5;
                rocket.drag -= rocket.drag / 5;
                oneOver = false;
            }
            if (DistanceEarth_ > (atmosHeight / 5) * 2 && twoOver && DistanceEarth_ < (atmosHeight / 5) * 3)
            {
                Gravity -= Gravity / 4;
                rocket.drag -= rocket.drag / 4;
                twoOver = false;
            }
            if (DistanceEarth_ > (atmosHeight / 5) * 3 && threeOver && DistanceEarth_ < (atmosHeight / 5) * 4)
            {
                Gravity -= Gravity / 3;
                rocket.drag -= rocket.drag / 3;
                threeOver = false;
            }
            if (DistanceEarth_ > (atmosHeight / 5) * 4 && fourOver && DistanceEarth_ < (atmosHeight / 5) * 5)
            {
                Gravity -= Gravity / 3f;
                rocket.drag -= rocket.drag / 3f;
                fourOver = false;
            }
            
            if (DistanceEarth_ > atmosHeight && fiveOver && DistanceEarth_ < (atmosHeight / 5) * 6)
            {
 
                rocket.drag -= rocket.drag;
                Gravity -= Gravity / 3f;
                fiveOver = false;
            }
            
            if (DistanceEarth_ > atmosHeight * 50)
            {
                Gravity -= Gravity;
         SphereOF_influ_out = false;
 
        
    }
 
        }




Comment
Add comment · Show 10
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Captain_Pineapple · Feb 14, 2019 at 12:33 PM 2
Share

Hey there,

first it might be helpfull to give some more information on what is happening and what you are actually axpecting to happen. ("it should orbit" is a bit vague)

Assu$$anonymous$$g that i actually understood your problem: Your current code will naturally not result in a stable orbit. To achive that you should read into the calculation of centrifugal forces to actually hit the right value for your gravity and/or speed to achive a stable orbit. Just applying some random gravity value will result in undefined behaviour.

avatar image chuck16 · Feb 14, 2019 at 09:57 PM 0
Share

So in my rocket game you are piloting a 3D rocket. As you go up in the atmosphere, drag and gravity go down. Drag goes down quicker than gravity. So when you get to a point where there is zero drag and a little gravity you should orbit around the earth just like real life. $$anonymous$$y desire is for this script to give me that outcome.

@Captain_Pineapple

avatar image chuck16 · Feb 14, 2019 at 09:59 PM 0
Share

I will read the wiki page and come back to you

avatar image chuck16 · Feb 14, 2019 at 10:18 PM 0
Share

I still don't get why I my script does not work for this. I don't totally understand the Wikipedia article either. Just applying some random gravity value will result in undefined behaviour. I dont get why I cant just put some random gravity value either?

avatar image Captain_Pineapple chuck16 · Feb 15, 2019 at 07:52 AM 0
Share

Can you perhaps put a more detailed description (perhaps even with a picture) of the desired and the current behaviour up? Then it might be easier to understand what your definition of "orbit" and "not working" is.

Also you want to move the rocket.AddForce to the FixedUpdate. Applying force in Updates and then even without time compensation will result in unforseeable and performance related behaviour...

avatar image chuck16 · Feb 15, 2019 at 11:24 AM 0
Share

Okay I will put .AddForce() in FixedUpdate and Here is a model I made to show what I want and what I need: alt text

@Captain_Pineapple

ipiccy-image.png (85.1 kB)
avatar image Captain_Pineapple chuck16 · Feb 15, 2019 at 01:35 PM 1
Share

And this is why rocket science is not this simple ;) The problem here is that just adjusting gravity will not be enough. When gravity is constant you will be bound to a loop that is given by your current velocity and position. If your speed and position matches the gravity given the formula for centrifugal/petal force then you will have a circular orbit. If it does not you will get an ellipse. This is just how it works. So basically your problem is not your concept of applying gravity but the issue that you have to adjust your speed at a specific time to a specific value (again here the calculation is needed) to achive a circular orbit.

@DCordoba did a nice job i think in simplifying your script but it will as far as i can see this still contain the same problem. You cannot simply say drag is none and gravity is constant and assume that you result in a circular orbit.

avatar image chuck16 Captain_Pineapple · Feb 15, 2019 at 02:19 PM 0
Share

Okay I think I got it. I was looking at it too simply. thank you. I will try to implement @DCordoba script changes thanks.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by DCordoba · Feb 15, 2019 at 01:18 PM

your planet is rotating right? so you can resume it on a single (simplified) calculus

  //about Rocket
  public Rigidbody rocket;
  public float DistanceEarth_;
  public float rocketMass;
  public Transform rocketT;
  //about Planet
  public Transform _Planet;
  public float GravityOnSurface;
  public float surfaceToCenter = 246601.54f;//distance from center to surface
  public bool SphereOF_influ_out = true;
  //used to check rotation of active planet 
  public vector3 prevForward = Vector3.zero;

   void Start(){
       prevForward = _Planet.forward;
  }

  // Update is called once per frame
  void Update()
  {
     Vector3 totalForce  = vector3.zero;
      //ditance to the center? otherwise change it to measure the distance to center on your scene
      DistanceEarth_ = Vector3.Distance(rocketT.position, _Planet.position) - 246601.54f;
      //if your planet rotation is constant, better store it on planet script, to not do it every frame
      Vector3 tangentRot = (_Planet.forward-prevForward)*Time.DeltaTime;

      float fCentripetal = (tangentRot.sqrMagnitude / DistanceEarth_)*rocketMass;
      float fGravity = GravityOnSurface * (surfaceToCenter/DistanceEarth_)*(surfaceToCenter/DistanceEarth_);

     //multiply the forces by their respective direction vectors 
     //(maybe this can be optimized on some way doing all together using vectors?)
      Vector3 totalForce = fCentripetal*tangentRot.normalized + fGravity * (_Planet.position - rocketT.position).normalized;
      rocket.AddForce(totalForce);
      
    }

If I did all the calculus well, this should work, (Im not sure to add rocket mass there since rigidbody also add it)

you can ignore the gravity force when is very low, to evade unnecessary calculations (`fGravity` is reduced by squared proportional distance, fCentripetal is linear, at some point you start just rotating, at other, far point from the first, planet just can't drag you enough to be measured)

source: http://hyperphysics.phy-astr.gsu.edu/hbase/orbv.html#eg

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

120 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Orbit controlled by virtual joystick 1 Answer

Faux Gravity help 0 Answers

How do I implement orbits between two GameObjects with Physics? 0 Answers

Issue with navmesh and object graphics 0 Answers

How to change the intensity of shadows in Unity3D 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges