1
\$\begingroup\$

How to change the gravity of a ball upward with a click Then change it again downward with another click And so on In unity

\$\endgroup\$
5
\$\begingroup\$
void ReverseGravity()
{
     Physics2D.gravity = new Vector3(0, -1.0F, 0);
}

void ApplyGravity()
{
     Physics2D.gravity = new Vector3(0, 1.0F, 0);
}
//if you are not using 2D then use Physics.gravity = new Vector3(0, 1.0F, 0);
//you could also  use Rigidbody AddForce to get your desired effect without changing any physics.
//you can set up a bool normalGravity and use something like this in a LateUpdate()
        if(Input.GetKeyDown(KeyCode.Mouse0))
        {
            if (normalGravity)
            {
                normalGravity = false;
                ReverseGravity();
            }
            else
            { 
                normalGravity = true;
                ApplyGravity();
            }
        }
|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$
Physics.gravity *= -1.0f; // :P
|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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