Unity: Enable/Disable Gravity from C# Script

Last updated on September 18, 2021 A Goodman Loading... 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
10 months ago

e

You May Also Like