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\$
1

2 Answers 2

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();
            }
        }
\$\endgroup\$
0
\$\begingroup\$
Physics.gravity *= -1.0f; // :P
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .