What to do to set gravity in unity? I'm working on a game called "OXYGEN", and for the space scene. I want to set the gravity of the Controller to 0.38, or Mar's gravity.

  • \$\begingroup\$ To clarify, are you working on a human scale, where we can treat gravity as having a constant direction and magnitude? Or on a planetary scale, where gravity might be different for each object depending on where it is in space? \$\endgroup\$ – DMGregory Oct 22 '17 at 18:29
  • \$\begingroup\$ Second one, please. \$\endgroup\$ – FlyBox Oct 22 '17 at 18:41
  • \$\begingroup\$ Alright, that gets a lot more complicated. The built-in character controller is only really built to handle constant gravity, and might not behave sensibly when the direction of gravity changes continuously. Can you tell us a bit more about what the character does in the game and how they should control? We can provide tailored guidance depending on the type of gameplay the gravity needs to influence. \$\endgroup\$ – DMGregory Oct 22 '17 at 18:44

You can set the global default Gravity vector in the Physics Manager. You can find it under Edit -> Project Settings -> Physics (or Edit -> Project Settings -> Physics2d for a 2d game).

If you want to change the gravity from a script while the game is running, set Physics.gravity to a new Vector3 (For a 2d game: Physics2D.gravity to a new Vector2).

If you have a more complicated gravity setup with zones with different gravity or point gravity sources, then you should set the global gravity to 0:0:0 and handle gravity acceleration on your own. I posted a simple script for a point gravity source here.


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.