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 RandomStrangr74 · Oct 01, 2018 at 12:30 PM · physics3dorbit

How do I implement orbits between two GameObjects with Physics?

I have made a script in my gravity-based spaceflight game; when the ship is aimed and launched by the player, it needs to be able to orbit around the planet. if it enters the planet's sphere of influence (shown by the misty sphere), provided it's not too close to crash, or too far away to be affected by gravity.

The following code below controls the gravity, enabling the ship (controlled by the player) to be affected by gravity within a specific range of the green planet (as determined by a child object's sphere collider). However, while the ship curves as a result of gravity, it does not orbit:

     void FixedUpdate()
             {            
                 Collider[] cols = Physics.OverlapSphere(transform.position, gravRange);
                 List<Rigidbody> bodies = new List<Rigidbody>();
         
         
                     foreach (Collider c in cols) 
                 {
                         Rigidbody collidingRB = c.attachedRigidbody;
                         if (collidingRB != null && collidingRB != rb && !bodies.Contains (collidingRB) && c.attachedRigidbody.CompareTag ("Player")) 
                     {
                         bodies.Add (collidingRB);
                         Vector3 offset = transform.position - (c.transform.position);
                         Vector3 forceToApply = (offset / offset.sqrMagnitude * rb.mass);
                         collidingRB.AddForce (forceToApply);
                         transform.RotateAround(transform.position, c.transform.position, 0f);
         ///<summary>
         ///Automatically calculate Planet Mass from Gravity range, so the ship will be gradually drawn in (at the edge, gravity = 0, and increases into planet).

 ///To simulate gravity, I opted to change the mass of the planet, as I couldn't figure out how to change the gravitational values, and for the purposes of the game, this was considered to achieve the same end.        
         ///</summary>        
 
 
                         planetMass = 80 * gravRange - 140;
                         rb.mass = planetMass - (offset.sqrMagnitude * (Mathf.Pow(4, 2)*0.9f));

///This part was added as an attempt to control the strength of gravity to attempt to create an orbit by prevent the ship from crashing into the planet immediately. While it enabled the ship to be pulled in slightly once reaching the sphere of influence (rather than being negligible on the edges), the ship still wouldn't orbit. if (rb.mass <= 3) { rb.mass = 3; } if (rb.mass >= 50) { rb.mass = 50; } } } }

Is there a way for the ship to be able to orbit around the planet when fired into its sphere of influence (while allowing it to crash if it gets too close, or shoot off if too far away)?

Note that in my case, I used 3D physics for this game. I had issues with implementing 2D physics bodies when I implemented them, and I would need to redo the entire game to insert 2D physics.

Here is an example of what I'm aiming for. The red lines represent an example of how I would like the ship to orbit within the green sphere of influence. The ship needs to be able to orbit when shot within the planet's sphere of influence

capture.png (185.9 kB)
Comment
Add comment · Show 2
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 Zodiarc · Oct 01, 2018 at 12:45 PM 0
Share

$$anonymous$$aybe the ship is just going to fast to enter the orbit? I'm no expert on that but maybe a read on Orbital $$anonymous$$echanics could be helpful.

avatar image hexagonius · Oct 01, 2018 at 01:09 PM 0
Share

in addition, you're making a game right? So it would be wise to implement a certain angle/ speed correlation that forces the ship into a perfect orbit, without physics, as it would give the player a clearer feedback if he did it right. I'd needs to feel good, not be physically aqurat

0 Replies

· Add your reply
  • Sort: 

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

168 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 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

How to stop sphere from clipping through cube edges aside from lowering Time.FixedDeltaTime? 1 Answer

Make objects stack in unity? 0 Answers

Player Car Runtime Scaling Issue 0 Answers

Ellipticall Orbit like Spaceflight simulator 1 Answer

How to go about filling the gaps between segmented objects connected with hinge joints? 0 Answers


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