Changing Gravity Quickly?

Hi, all!

I’m experimenting with my very first iOS game, and it is going nicely. Currently, the player falls, and the gravity on the X axis is controlled by the tilt of the phone.

However, when the user tilts the phone in the opposite direction of the current gravity, it takes a long while to get the gravity to reverse.

Is there a way that I can get the gravity acting on the cube to change more quickly? (I would love to be able to control how responsive it is)
Maybe a way to just reset the rigid body?

Current code:

public float force;

void FixedUpdate () {

		Physics.gravity = new Vector3( Input.acceleration.x*force, -9.81f, 0 );

Thanks! - YA

While I understand your question in general, figuring out the right fix right fix will require experimenting on your part. I can think of half a dozen different approaches to the problem. Let me suggest three of them.

Try #1:
First, try playing with drag and the strength of gravity. How quickly something moves in a new direction is partly how long it takes for existing velocity to decay and partly the strength of the new force. You may also have to place a limit on the velocity. You can do that by putting this line in FixedUpdate():

rigidbody.velocity = Vector3.ClampMagnitude(rigidbody.velocity, maxVelocity);

Try #2:
You can just redirect the velocity so that it is always aligned with gravity. Put this in FixedUpdate():

rigidbody.velocity =  Physics.gravity.normalized * rigidbody.velocity.magnitude;

You may also have to limit velocity with this solution.

Try #3:
You can add some extra force if the object is moving in the wrong direction.

if (Vector3.Dot(rigidbody.velocity, Physics.gravity) < 0.0) {
    rigidbody.AddForce(Physics.gravity * someFactor, ForceMode.Acceleration)

Bonus suggestion: Similar to #2 but using Vector3.Project(). In FixedUpdate():

rigidbody.velocity = Vector3.Project(rigidbody.velocity, Physics.gravity);