Setting the gravity of an object

Hi guys!

How can I set the gravity of an object in an event that gets called when you touch/click at the screen?

Thanks

Get the Rigidbody from the object, and set “useGravity” to true.

3 Likes

If you want to change object mass in runtime use Rigidbody.mass with the object you want interact:

The OP wants to change the gravity experienced by the object, not the mass. Changing the mass of an object would not affect the acceleration experienced by it.

I find it strange that there is no 3D equivalent of Physics2D.gravity (or maybe I just haven’t worked with 3D enough!). I think the solution to the OP’s problem is using Rigidbody.Addforce with Forcemode.Acceleration.

2 Likes

There are two ways to add gravity to an object. The method mentioned by @Jaimi is the way to apply gravity on a global scale. If you want a different gravity setting for one or more objects though, you can add a Constant Force at runtime and adjust accordingly.

public ConstantForce gravity;
gravity = gameObject.AddComponent<ConstantForce>();
gravity.force = new Vector3(0.0f, -9.81f, 0.0f);

Keep in mind if you use the local method without disabling the global method for your object (through useGravity), you’ll have both effects at once.

gameObject.GetComponent<Rigidbody>().useGravity = false;
8 Likes

Just add an exponential down force …

public float GravityMultipler;
float AddGravity;
    void Update()
    {
            AddGravity += GravityMultipler;
            _rigidbody.AddForce((Vector3.down * AddGravity), ForceMode.Acceleration);

......

So you can maintain normal gravity to everything else.

1 Like

For anyone still looking for this, check this out:

2 Likes