Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to make an object fly which has ridged body physics by disabling its gravity after pressing a key. So I just want to know how to disable gravity through C# script. I already went through google search but I didn’t get what I want. I even tried this link http://docs.unity3d.com/ScriptReference/Rigidbody-useGravity.html

share|improve this question
4  
other.attachedRigidbody.useGravity = false; - Is it that hard to read? – Ethan Bierlein May 26 '16 at 17:11
up vote 3 down vote accepted

You need to do something like this. its simple

    public GameObject YourGameobject;//refrence of your gameobject
    void Update()
    {
    if (Input.GetKey(KeyCode.W))//on W input it will disable the gravit of your desire object
        {
            YourGameobject.GetComponent<Rigidbody>().useGravity = false;
        }
    }

You can change KeyCode.W into according to your requirement

share|improve this answer
    
Your code works. Thanks a lot. – simon143 May 27 '16 at 9:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.