Unity: Enable/Disable Gravity from C# Script

Updated: September 18, 2021 By: A Goodman One comment

In Unity, you can programmatically enable or disable gravity for an object from your C# script as below:

this.GetComponent<Rigidbody>().useGravity = true; // Enable
this.GetComponent<Rigidbody>().useGravity = false; // Disable

Example

The following code will turn on gravity for an object 3 seconds after the game starts:

void Update()
  {
    if (Time.time >= 3)
    {
      this.GetComponent<Rigidbody>().useGravity = true;
    }
}

You can find more details about Rigdibody.useGravity in the official docs. Happy coding!

Subscribe
Notify of
guest
1 Comment
Inline Feedbacks
View all comments
wew
wew
2 years ago

e

Related Articles