how to Apply Gravity at Run time ..?????????

Hello Frends…
I m developing a game in my game when i click on mouse button then apply Gravity to particular object…

                         please a give me some idea.............

Thanks a lot.

2 Answers

2

It depends on what are your objects, and what are you trying to do. If the objects are rigidbodies, and you intend to use the standard gravity (Physics.gravity), you can just set rigidbody.useGravity to true/false when needed. On the other hand, if you need a different gravity orientation or value, let useGravity = false and apply a force at FixedUpdate when enabled by the click:

var localGravity: Vector3 = 9.8 * Vector3.up; // inverted gravity
var gravityOn: boolean = false;

function OnMouseDown(){
    gravityOn = !gravityOn; // toggle the gravity when clicked
}

function FixedUpdate(){
    if (gravityOn) rigidbody.AddForce(localGravity);
}

Thanks a lot for help aldonaletto once again thanks........

@Lavpatel If it did work, you should accept the answer as working. That will make it easier for people to get the correct/working answer for this question, if they get the same problem! /TheDDestroyer12

You need to attach a rigidbody. Make sure it has gravity enabled.