Kinda Code
Home/C Sharp/Unity: Enable/Disable Gravity from C# Script

Unity: Enable/Disable Gravity from C# Script

Last updated: September 18, 2021

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!