- Home /
How to turn off gravity?
I'm working on a top-down game in unity, which is using 2d. However, I can't find a way to turn off gravity. Any help is much appreciated. Thanks in advance!
Gravity is only off when no scripts are attached, am I missing something?
Are your scripts adding downward force to the object? Turning off gravity does not effect any force being applied by your scripts.
Turns out it was my code, thanks for the help everyone!
Still, the answers below are correct for disabling gravity. Through settings, and through code. You should accept one of them as an answer.
Answer by chrismcrae5712 · Feb 24, 2014 at 02:48 AM
Try Edit > Project Settings > Physics and turn Y to 0. That'll make the Y Gravity 0 thus turning off gravity. Not sure if that's what you were looking for though. Hope it helps!
Answer by robertbu · Feb 24, 2014 at 02:46 AM
You can turn off gravity individually on each Rigidbody. In addition you can turn off gravity globally by:
Edit > Project Settings > Physics2D
Or in code :
Physics2D.gravity = Vector2.zero;
I'm not sure why,but neither method works for me. Thanks for the help tho!
You are using 2D. I missed that. I've edited my answer for 2D.
Perfect answer. Thanks!
edit: although Physics2D.gravity is actually a 2d vector, so others should be aware of that.
Answer by Michio-Magic · Oct 18, 2015 at 06:06 PM
How to turn it on and off ...
This worked for me (attached script to game object).:
public static var rigidbod : Rigidbody;
function Awake () { // initialise the variable
rigidbod = GetComponent(Rigidbody);
}
function Update () {
rigidbod.useGravity = false;
// rigidbod.useGravity = true;
}
Hi!
Im using your cript. I have this gravitygun and a ball. How do i make it that only when i hold the ball gravity is off. But then i can shoot the ball and i want gravity to turn back on.
Sry for my hard to read english..
Is this Java? I am trying this as Java but it is not working.