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 LAZYGAMES_ · Jan 19, 2018 at 07:10 PM · 2dscripting problemphysicsgravity

Question on 2d planet gravity

Hi everyone! I am working on a 2d game where the player is spinning around a planet. While he spins he is supposed to get closer to the planet as longer he spins. Then he can thrust himself out of the gravity of planet 1 and get pulled to planet 2 if he gets the right moment to thrust.
Now my question is how I can get the player spinning around the planet while getting closer to him. Also I have no idea how to manage to get the player tangential out of the gravity of the planet.
I kind of put everything together in an illustration.alt text

As im pretty bad in things like physics in #C I would be very happy if someone who has a bit of knowledge about physics in unity could come up with an idea or code.

picture.png (33.0 kB)
Comment
Add comment · Show 1
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 nt314p · Jan 20, 2018 at 08:36 PM 0
Share

By spinning do you mean orbiting?

"he is supposed to get closer to the planet as longer he spins" Does that mean his orbit is getting smaller or decaying?

"the player spinning around the planet while getting closer to him" Not even going to start.

I want to offer help, but its evident that you don't even understand basics of orbital mechanics. I mean its not like its rocket sci-

Go to the internet, read a bit, and then come back. I will be happy to help you then.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Kishotta · Jan 20, 2018 at 08:57 PM

There are at least 2 ways of going about this (one performant, the other physically accurate).

  1. Spheres of Influence (SOI). Each gravitational body (planet) has a sphere (circle in 2d) around it, inside which all orbiting bodies (player/satellite) are pulled towards the center with a constant force. This is the performant approach because you only ever deal with one SOI per object at one time. The caveat here is that you have to take care to ensure SOI's don't overlap. Or if they do, you'll have to assign one precedence over the other. As well, you can have large areas in between gravitational bodies where gravity does not affect your orbiting bodies at all.

  2. N-body simulation. Each gravitational body is constantly pulling every orbiting body towards the gravitational center with a force proportional to the distance squared between the objects. This means that each satellite is being pulled in N directions at once (hence the name) but in varying amounts. This is how the universe actually works. However, it can be computationally expensive to calculate for large numbers of planets. I'd imagine it would also be a nightmare to try to balance/test/adjust levels so that they work the way you want them to.

Now on to your actual question, the decaying orbits (slowly spiraling in towards the planet) is a side effect of your object slowing down while orbiting (a constant speed would mean a constant orbit). If you go with the SOI approach, you could increase the drag on your object so that it would start slowing down while inside the sphere of influence, simulating atmospheric friction. I wouldn't imagine you'd need a lot of drag to get the effect you want, but tune to taste. You'd of course need to reset the drag to 0 when you leave the SOI.

As for calculating the direction of gravity at any given point (again assuming SOI implementation) if you take the position of the planet and subtract the position of the satellite, you will get a vector in the direction towards the center of the planet from the satellite's point of view. You'll need to calculate that every frame (or physics step, just use FixedUpdate instead of Update) and apply the force in that direction.

Comment
Add comment · Show 1 · 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
avatar image LAZYGAMES_ · Jan 21, 2018 at 05:26 PM 0
Share

@$$anonymous$$ishotta

Hi and thank you for your awnser.
I tried to build a SOI like you described. For that I deactivated the standard gravity of Unity and added a PointEffector2D to the Planet. Then i managed to get the player rotating around the planet using the RotateAround function like this:

 public float speed;
  
  public Transform target;
 
     private Vector3 zAxis = new Vector3(0, 0, 1);
 
     void FixedUpdate()
     {
         transform.RotateAround(target.position, zAxis, speed);
     }



This script get activated when the player enters a collider which is placed around the planet. Like that both forces are overlapping and the player gets pulled towards the planet while orbiting around him when he gets near him.
But I still have two problems. The first one is that the player instantly starts rotating around the planet when he enters the collider ins$$anonymous$$d of fading in smoothly. alt text And the second problem is that i still have no clue how to get the player out of the orbit when he hits a button. I tried to add Force to the direction the player faces but i have not managed to get this to work. Is there a simple solution for this problem?
I want to thank you again for the time yoou have taken to awnser my question and I would be glad if you would have an idea how to solve those two problems.

picture2.png (88.8 kB)

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

231 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Object jumps right after the attached HingeJoint2D is Enabled 0 Answers

Why is there no gravitational acceleration with Unity 4.3.1 2D? 0 Answers

Coroutine not working properly 1 Answer

Need to restrict "air control" 2D platformer. 1 Answer

Limit player speed (2D) 3 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