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 /
  • Help Room /
avatar image
0
Question by PT_Zef · Sep 24, 2019 at 02:51 PM · physicsgravityorbitsimulationplanet

I need help with my solar system simulation.,I need help with getting my phsics simulation to work properly.

Okay, so I have been trying to simulate a solar system in unity, and the way I'm going about it is giving me headaches. Instead of setting up fixed orbits around the sun, I have instead tried to implement a gravity system so that everything orbits the sun based on their mass and their velocity. However I'm having issues getting the velocity part to work. The mass works fine and if you press play items with large mass like the sun very quickly attracts everything within its reach, but nothing starts orbiting, it always clumps together. To try to solve this I added in the variable Velocity. I created an empty game object and parented it to the planet I'm trying to get to orbit. The purpose of this is because I want the planet to rotate on its axis, but at the same time, i need the velocity force to be applied to the planet perpendicular to the pull of the suns mass at all times. the empty game object is set to constantly face the sun, in the hopes that i can call its x axis to apply force to the planet, since the x axis of the empty game object will always be perpendicular to the suns gravitational pull. that is the theory anyways, but in practice, when I hit play, it seems that the force isn't applied to the desired direction, and the planet ends up sling shotting off of the sun and gets flung into oblivion. Any help with this would be appreciated, I know there are far easier ways to get a planet to orbit, but for the purpose of my project, i need it to be in a free orbit based on gravity. Here is the code I am using for this:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Attractor : MonoBehaviour
 {
 
     const float G = 667.4f;
     public GameObject thrustLock;
     public float velocity;
     public static List<Attractor> Attractors;
 
     public Rigidbody rb;
 
     void FixedUpdate()
     {
         foreach(Attractor attractor in Attractors)
         {
             if(attractor != this)
             {
                 Attract(attractor);
             } 
         }
     }
 
     void Update()
     {
         rb.AddRelativeForce(thrustLock.transform.right * velocity);
     }
 
     void OnEnable()
     {
         if(Attractors == null)
         {
             Attractors = new List<Attractor>();
         }
 
         Attractors.Add(this);
     }
 
     void OnDisable()
     {
         Attractors.Remove(this);
     }
 
     void Attract (Attractor objToAttract)
     {
         Rigidbody rbToAttract = objToAttract.rb;
 
         Vector3 direction = rb.position - rbToAttract.position;
         float distance = direction.sqrMagnitude;
 
         if(distance == 0f)
         {
             return;
         }
 
         float forceMagnitude = G * (rb.mass * rbToAttract.mass) / distance;
         Vector3 force = direction.normalized * forceMagnitude;
 
         rbToAttract.AddForce(force);
     }
 }

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

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

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

Unity Faux gravity on a cube: reaching some faces is troublesome 0 Answers

Gravity on a cube planet 1 Answer

Moving and Rotating a Player Smoothly on a Rotating Planet 0 Answers

I need to create artificial gravity ( like a fake fake centrifugal effect) 2 Answers

How do i prevent my character from spinning uncontrollably on a planet? 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