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 lancegerday · Apr 18, 2012 at 12:06 AM · 3dgravityplanet

3D Planatary Gravity

I am using RigidBodies and want my planets to use real physics bases off of their mass and want to have that gravity to interact with other RigidBodies in the system.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by syclamoth · Apr 18, 2012 at 12:42 AM

Step 1: Turn off gravity

Step 2: Create some kind of 'forcefield' script on your planets. There are a lot of ways of doing this, but the simplest one would be to use Physics.OverlapSphere and then apply forces to everything in that area. Here's a sample:

 // Make sure to add the line 'using System.Collections.Generic'
 // on the first line of the file

 public float forceRadius = 20;
 public float gravPower = 9.81f;
 void FixedUpdate()
 {
     // Populate a list of nearby bodies
     List<Rigidbody> bodies = new List<Rigidbody>();
     foreach(Collider col in Physics.OverlapSphere 
         (transform.position, forceRadius))
     {
         if(col.attachedRigidbody != null && !bodies.Contains(col.attachedRigidbody))
         {
             bodies.Add(col.attachedRigidbody);
         }
     }
     // Now you have your rigidbodies, time to add the force!
     foreach(Rigidbody body in bodies)
     {
         float bodyDist = (body.position - transform.position).sqrMagnitude;
         float gravStrengthFactor = Mathf.Pow(forceRadius) / bodyDist;
         body.AddForce(gravStrengthFactor * gravPower * 
            (transform.position - body.position) * Time.deltaTime, ForceMode.Acceleration);
     }
 }

Of course, there are a whole slew of optimisations you can apply to this, but it's a start.

Comment
Add comment · Show 3 · 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 syclamoth · Apr 18, 2012 at 12:43 AM 0
Share

Step 3: Write a simplified physics engine to simulate interactions between the large 'gravity generator' objects, since they would kind of break themselves if they could affect themselves.

avatar image kolban · Apr 18, 2012 at 01:53 AM 0
Share

Would applying forces not produce "repulsion" as opposed to attraction? Wouldn't gravity have to do solely about attraction?

avatar image syclamoth · Apr 18, 2012 at 02:03 AM 1
Share

What's the difference between 'repulsion' and 'attraction'? The direction of the force, that's what. Have you tried the script? I think there are some issues with the falloff calculation, but the basic concept is sound.

avatar image
0

Answer by kolban · Apr 18, 2012 at 12:29 AM

Unity's physics engine only simulates gravity from one gravitational body. The RigidBody objects have a force applied to them in the down (-ve Y) axis only. There is no concept of creating additional gravity fields/forces that would act as attractors to other objects.

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

6 People are following this question.

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

Related Questions

Creating 3d planets with terrain and atmosphere 3 Answers

How to make a character walk on a 3D cube planet and a long 3D cube planet using gravity? 2 Answers

Planet gravity help need guidance no code 1 Answer

How do I add gravity in code? 1 Answer

How to stop a mesh from falling through 3D objects. 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