- Home /
How can you change the gravity variable depending on your Scene?
I'm working on a science fiction game but I need some help with one thing gravity. The game is set on multiple planets and needs to have the gravity variable change to be set accordingly. Could somebody send me a script of how to do that?
Answer by unidad2pete · Aug 23, 2017 at 12:29 PM
print(Physics.gravity) // prints the current gravity, default gravuity is Vector3(0, -9.8, 0) that means force on Y axis to down.
Physics.gravity = new Vector3(0, 9.8f, 0); // changes to new gravity, in this example, 9.8 on Y axis to up(reverse to default)
So if I add to this to the player will it reset the gravity value every scene?
Also if I how would I write this in $$anonymous$$onoDevelope?
Answer by Bunny83 · Aug 23, 2017 at 11:00 PM
Just create a simple script like this:
// SetGravity.cs
using UnityEngine;
public class SetGravity : MonoBehaviour
{
public Vector3 gravity = new Vector3(0, -9.8f, 0);
void Start()
{
Physics.gravity = gravity;
}
}
Just add this script to any object in each scene and set the gravity you want in the inspector. When the scene is loaded (and therefore the script on the object in the scene) it will automatically set the gravity to the desired value.
Your answer
![](https://koobas.hobune.stream/wayback/20220612133351im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
what are the basic rules off making a lan server 1 Answer
How can I slow down to stop(HumanoidIdle) character when getting close to a object using timeline ? 0 Answers
How can i make a text changed event in EditorGUILayout.TextField ? 1 Answer
When moving object, should i use transform.position or a animation clip i recorded ? 0 Answers