- Home /
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.
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!
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.
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!
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.
Your answer
Follow this Question
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 can I rotate and move a cylindrical rod from one side with respect to other side? 1 Answer
Gravity only rigidbody 1 Answer