Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 xSpectrum · Dec 06, 2012 at 04:47 AM · movementphysicsgravity

How to program gravity for planets?

I want to make a 2-D platformer/puzzle game involving multiple small "planets" similar to Super Mario Galaxy. How would I go about programming gravity for these planets so that the closest large planet pulls the player towards it (like in real life)?

Also, I'm running into a bit of trouble programming the movement script for this as well. So if anyone knows how to move the player in a circular motion, that'd be great.

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 DayyanSisson · Dec 06, 2012 at 05:16 AM 0
Share

You should post some code you've tried out. Or do you not know where to start?

3 Replies

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

Answer by Dave-Carlile · Dec 06, 2012 at 01:50 PM

This is the formula for calculating gravitational force: gravitational_force = gravitational_constant ((mass1 mass2) / squared_distance_between_objects)

 foreach planet
   gravitational_constant = 100  <-- make something up here until you get pleasing results
   distance_squared = (planet.position - player.position).sqrmagnitude
 
   // get the masses - note that the planet mass needs to be much larger than the player mass
   // and make sure these values are set on the rigidbody so the Unity physics will act correctly
   // when the force is applied
   mass1 = planet.mass = 1000    <-- mess around with these until you get pleasing results
   mass2 = player.mass = 1       <-- this can probably always stay 1 for the player
 
   force = gravitational_constant * ((mass1 * mass2) / distance_squared);
 
   // e.g. plugging in the values...
   // force = 100 * ((1000 * 1) / distance_squared)
 
 
   // apply the force from the player toward the planet
   force_direction = (planet.position - player.position).normalized;
   force_vector = force_direction * force;
 
   // TODO : use AddForce or whatever to apply force_vector to the player's rigidbody
   // you end up adding each force vector to the player so it will move toward the 
   // strongest/closest/most_massive planet.
 

 
 

Basically the force is the object masses multiplied together, then scaled by the distance between the bodies, then adjusted by the gravitational constant. You can change the mass of each planet based on its sized to make larger planets have a larger attraction.

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 xSpectrum · Dec 08, 2012 at 12:10 AM 0
Share

Thanks much! However, do you know how I would go about applying a normal force to the player when they are on the "planet" so that they don't go straight through the planet?

I currently am using a rigidbody on my player, and using AddForce to apply the gravity. In a different script, I have a Raycast casting towards the center of the planet, but this isn't working:`rigidbody.AddForce(hit.normal*$$anonymous$$ars.GetComponent(Gravity).force)`. Where I have your gravity script saved as "Gravity" and applied to $$anonymous$$ars (a previously declared variable of my planet). Once the player reaches the surface of the planet, it will slow the player down, but not enough to completely stop it. If I lower the gravity, the player just bounces up and down on the surface.

avatar image
0

Answer by MountDoomTeam · Dec 06, 2012 at 08:50 AM

you can add a constant force pulling the player to the centre of the planet, and if the player is a certain distance from the planet, the constant force can be turned off or reduced relative to distance, like that he will always be attracted to the centre of the planet and if he goes far enough away he will be in free space. it also means that if you freefall in the planet's atmosphere you will accelerate into the ground and if you jump in the air it will be the same as gravity, except towards the centre.

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

Answer by MountDoomTeam · Dec 06, 2012 at 08:50 AM

you can add a constant force pulling the player to the centre of the planet, and if the player is a certain distance from the planet, the constant force can be turned off or reduced relative to distance, like that he will always be attracted to the centre of the planet and if he goes far enough away he will be in free space. it also means that if you freefall in the planet's atmosphere you will accelerate into the ground and if you jump in the air it will be the same as gravity, except towards the centre.

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

13 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

Related Questions

Do character controllers work with dynamic gravity? 1 Answer

How do I get the platform to effect these coins? 2 Answers

Adding gravity to character and grounded checks are not working? 0 Answers

Player slides down slope. 0 Answers

CharacterController.Move uses gravity even though it shouldn't 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