- Home /
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
Answer by robertbu · Apr 16, 2014 at 01:03 AM
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);
Your answer
![](https://koobas.hobune.stream/wayback/20220613142815im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Accelometer controls for iOS and android 2 Answers
Anyone know where i can find a tutorial for tilt scripts for iOs 1 Answer
Rigidbody.Force code working in editor, not on iPad (Gravity More on iPad?) 0 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
library not found for -lGoogleUtilities,library not found for -lGoogleUtilitiesf 0 Answers