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 OggDog · Nov 09, 2020 at 10:05 PM · gravitynot workinguniverse

Universe function on gravity script not working

So recently I had the craving to try and create world scapes revolving around gravitational pull, so I stumbled upon this youtube video (https://www.youtube.com/watch?v=7axImc1sxa0&t=281s) that did a great job explaining everything for me and gave me a sample code: using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using UnityEngine;

 public class PlanetBody : MonoBehaviour
 {
 
     public float mass;
     public float radius;
     public Vector3 initialVelocity;
     Vector3 currentVelocity;
 
     void Awake ()
     {
         currentVelocity = initialVelocity;
     }
 
     public void UpdateVelocity(PlanetBody[] allBodies, float timeStep)
     {
         foreach (var otherBody in allBodies)
         {
             if (otherBody != this)
             {
                 float sqrDst = (otherBody.GetComponent<Rigidbody>().position - GetComponent<Rigidbody>().position).sqrMagnitude;
                 Vector3 forceDir = (otherBody.GetComponent<Rigidbody>().position - GetComponent<Rigidbody>().position).normalized;
                 Vector3 force = forceDir * Universe.gravitationalConstant * mass * otherBody.mass / sqrDst;
                 Vector3 acceleration = force / mass;
                 currentVelocity += acceleration * timeStep;
             }
         }
     }
 
     public void UpdatePosition (float timeStep)
     {
         GetComponent<Rigidbody>().position += currentVelocity * timeStep;
     }
 }

Now this was all well and good, until I attached it to a sphere (or any other object) and unity kept saying that Universe was used in the wrong context.

Could anybody tell me the right context or how to fix this issue? Thanks in advance

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by andrew-lukasik · Nov 09, 2020 at 10:37 PM

You can replace Universe.gravitationalConstant with 6.67408e-11f.

Comment
Add comment · Show 4 · 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 OggDog · Nov 10, 2020 at 10:05 AM 0
Share

@andrew-lukasik - Your solution seems to have worked (I can't believe I didn't think to search up the universal gravitational constant!), but I'm quite lost on what to do revolving a second bit of code with similar issues:

  public class NBodySimulation : $$anonymous$$onoBehaviour
     {
     
         PlanetBody[] bodies;
     
         void Awake ()
         {
             bodies = FindObjectsOfType<PlanetBody>();
             Time.fixedDeltaTime = Universe.physicsTimeStep;
         }
     
     
         void FixedUpdate()
         {
             for (int i = 0; i < bodies.Length; i++)
             {
                 bodies[i].UpdateVelocity(Universe.physicsTimeStep);
             }
             for (int i = 0; i < bodies.Length; i++)
             {
                 bodies[i].UpdatePosition(Universe.physicsTimeStep);
             }
         }
     }
 

I have pretty much no idea of what to change Universe to in this code, so if you may have an answer, that'd be a lifesaver.

Again, thanks so much for the previous answer!

avatar image andrew-lukasik OggDog · Nov 10, 2020 at 10:47 AM 0
Share

$$anonymous$$ake sure THIS file/class is among your project files

avatar image andrew-lukasik andrew-lukasik · Nov 10, 2020 at 10:53 AM 0
Share

And then replace 6.67408e-11f with Universe.gravitationalConstant back again (since it's an arbitrary value there)

avatar image andrew-lukasik OggDog · Nov 10, 2020 at 12:29 PM 0
Share

Alternatively:

 public class NBodySimulation : $$anonymous$$onoBehaviour
 {
     [SerializeField] float _physicsTimeStep = 0.01f;
     PlanetBody[] bodies;
     void Awake ()
     {
         bodies = FindObjectsOfType<PlanetBody>();
         Time.fixedDeltaTime = _physicsTimeStep;
     }
     void FixedUpdate ()
     {
         for( int i=0 ; i<bodies.Length ; i++ )
         {
             bodies[i].UpdateVelocity( Time.fixedDeltaTime );
         }
         for( int i=0 ; i<bodies.Length ; i++ )
         {
             bodies[i].UpdatePosition( Time.fixedDeltaTime );
         }
     }
 }

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

149 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

Related Questions

How to make Object1 be upfront Object2 even though it is inside of it? 0 Answers

Newtonian Gravity 2 Answers

Make box tip over edge of platform 1 Answer

Enemy Ai Problem with the collider and gravity 1 Answer

When my object tilts back the foreword movement lifts it off of the ground. how do i keep it grounded 2 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