- Home /
(C#)How To Disable Gravity From Script?
Yes I know there are lot's of questions about this, but when I try to get them to work they give me errors when I use the most common way: Rigidbody.useGravity or Rigidbody2D.useGravity. It says that rigidbody or rigidbody2D doesn't contain a definition of useGravity. So how can I disable my gravity then? Is there a way to change the location of the gravity from rigidbody2D to my character script? Like a bool or something.
Answer by zharik86 · Aug 09, 2014 at 06:39 AM
Use reference on rigidbody:
//For rigidbody
this.rigidbody.useGravity = false;
//For rigidbody2D
this.rigidbody2D.gravityScale = 0.0f;
I hope than it will help you
I change gravityscale=0 in rigidbody2d component then It work for disable my character fall down But when I use velocity=new vector(5,0) for my character move horizontal it run straight away with no stop position.
Is there any solution for disable gravity can use addforce function and velocity for moving horizontal ?
Answer by Rugbug_Redfern · Sep 29, 2017 at 01:01 AM
GameObject.GetComponent<Rigidbody>().useGravity = false;
gameObject.GetComponent<Rigidbody>().useGravity = false;
First letter small case
Answer by Melann · Sep 28, 2017 at 06:13 PM
There is a new answer, and it is not documented in the Scripting API:
Turning off gravity from script.
This is what the Scripting documentation tells you to do: gameObject.rigidbody.useGravity = false;
This is the error message you get when you do that: Assets/Scripts/ReceiveTaps.cs(59,14): error CS0619: UnityEngine.GameObject.rigidbody' is obsolete:
Property rigidbody has been deprecated. Use GetComponent() instead. (UnityUpgradable)'
And this is how it works: gameObject.GetComponent ().useGravity = false;
Answer by chriswagie · Feb 08 at 11:16 PM
bruh you don't know me but i've been stuck trying to make an object drop on another object's destruction for a month now (I'm new, in the unity tutorials) and with your answer i was finally able to do it. Thank you soooooo much.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Disable a script on collision 2 Answers
Need a script 0 Answers
sound on key down help 2 Answers
How To Invoke RayCastAll? 1 Answer