• 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
6
Question by LukeNukem44 · Aug 18, 2018 at 03:59 PM · gameobjectphysicsrigidbodygravityrigidbody.addforce

How To Set Individual Rigidbody Gravity [Solved]

This is not a question, but a solution.

After about an hour of searching for this answer, I spent an hour working out the solution.

It is super simple.
Code:

     [HideInInspector] new public Rigidbody rigidbody;
 
     public bool useGravity = true;
 
     void Awake() {
         rigidbody = GetComponent<Rigidbody>();
     }
     
     void FixedUpdate() {
         rigidbody.useGravity = false;
         if (useGravity) rigidbody.AddForce(Physics.gravity * (rigidbody.mass * rigidbody.mass));
     }

If only the Rigidbody.useGravity is true, then it will fall as per the global Physics.gravity value.
AddForce(Physics.gravity) will achieve the exact same result if Rigidbody.useGravity is false.
If Rigidbody.useGravity is true, then both forces will be applied, so the Rigidbody.useGravity bool must be false to replicate and customize the object's gravity.

FixedUpdate() sets Rigidbody's useGravity to false to ensure it stays off.


Modify as desired. Using the Rigidbody.mass value is great.
However, if set to (Physics.gravity x rb.mass), nothing will change. It strangely seems like the gravity is first divided by the mass before completing the gravity x mass operation.

That is simply fixed by squaring the mass. (mass x mass)
If the mass is 2, the object will fall by gravity x 2 as expected.

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 LukeNukem44 · Aug 18, 2018 at 04:06 PM 0
Share

If gravity is multiplied by a custom value, that value must also be squared to achieve the expected result.


As i was reading other threads online, other people had the same issue of nothing happening because the modifier wasn't being squared.

I hope this helps a lot of people.
Have fun!

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by jeffk1945 · Jan 31, 2021 at 05:53 PM

This answer is wrong. If you want them to fall at the same acceleration or another acceleration value, to simulate say gravity on the moon.

Objects don't fall as a function of their mass.

 private Rigidbody rb;
     void Start()
     {
         rb=GetComponent<Rigidbody>();
     }

 void FixedUpdate()
     {
         rb.AddForce(Physics.gravity*rb.mass);
     }

If you want to give another value of gravity, say your game environment is the moon.

 private Rigidbody rb;
 public float gMoon=1.6f;  //gravity on the moon.
     void Start()
     {
         rb=GetComponent<Rigidbody>();
     }
     void FixedUpdate()
         {
             rb.AddForce(new Vector3(0, -1.0f, 0)*rb.mass*gMoon);  
         }

If you want to test yourself. Drop two rigidbodies side by side from the same height. One just has useGravity=true, the other set to false. Attach this artificial gravity script to the other and watch them fall side by side. Vary the mass of the artificial gravity rigicbody and you will see that the acceleration is independent of it's mass.

Make sure you turn on interpolation also for the artificial gravity. I ran this test as outlined above. It works.

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 TitanTreasures · Jan 24, 2020 at 11:48 PM

OMG thank you so much! This has annoyed me countless times that i have just accepted that no matter how big the mass, my rigid body woulden't fall any faster. Until now, thanks so much!

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 mscottveach · Sep 09, 2021 at 01:49 PM 0
Share

But it shouldn't fall faster due to having a larger mass. Not unless there are other factors like size and shape that affect the drag. But in a vacuum, all masses, no matter how small or large, will fall at the same rate.

avatar image
0

Answer by BKGcode · Dec 13, 2021 at 12:52 AM

Works for me! Thanks!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

200 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

Related Questions

Setting a RigidBody's velocity messes with my custom gravity, not sure how to proceed. 0 Answers

How to make rigidbodies on each side of a cube fall towards the cube? [multiple gravity / addForce] 0 Answers

How to keep a GameObject inside a collider? 3 Answers

Where is the general Rigidbodies Drag? 3 Answers

Player in Spaceship 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