Changing gravity direction...

I have a simple script in C# that changes gravity vertically on the press of a button. Here it is:

using UnityEngine;
using System.Collections;

public class GravitronThingy : MonoBehaviour {
	
	public bool Gravitronned = false;
	
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		
		if(Gravitronned = true && Input.GetKeyDown(KeyCode.Q)) {
			Physics.gravity *= 1;
		}
		
		if(Input.GetKeyDown(KeyCode.Q)) {
			Physics.gravity *= -1;
			Gravitronned = true;
		}
	}
}

How would I switch from side to side? how about front to back?

Thanks in advance!

Also the script stops working when the objects in the scene are still... :( any help with that?

5 Answers

5

Just store Physics.gravity in a temporary variable, then have it always be equal to that variable (Vector3). Then change the x, y, and z values of your gravity variable.

Vector3 gravity;

void Start()
{
    gravity = Physics.gravity;
}

void FixedUpdate()
{
    Physics.gravity = gravity;
    
    if(somethingIsTrue)
    {
        gravity.x = 0;
        gravity.y = -25;
        gravity.z = 0;
    }
}

This is a very helpful answer, there is only one thing. Whenever manipulating with physics you should do it in FixedUpdate funcation and not Update. Also if you plan on using Time.deltaTime in FixedUpdate, change that to Time.FixedDeltaTime. Documentation is very thorough if you require deeper explanation

Thanks for the additional information, +1.

Hello! Your comment seems to be very helpful, however it doesn't work for me, the vector 3 updates, but the gravity doesn't change at all, is there something else I need to do ? Thanks!

In unity you have (as you already know) Physics variable gravity that directs direction and force of gravity effect. It is simply a vector. So you can achieve all the effects you mention above by simply changing that vector to your needs. So to have it go left (assuming -X goes to your left)

Physics.gravity = new Vector3(-1.0f, 0, 0);

would do your job.

does this effect the games gravity or an individual objects gravity though?

Unity Gravity is a vector. The vector can point in any direction. You’re interested in the up/down, left/right, forward/back, which happen to correspond to the 3 axes.

Up and down use the y axis.
Left and right use the x axis.
Forward and back use the z axis.

The Vector struct in Unity has properties that correspond to these axis directions.

Vector3.forward, Vector3.up, and Vector3.right.

So to set your gravity to the right, set it to Vector3.right * gravityAmount, where gravityAmount is the amount of force. To set it to the left, use -Vector3.right * gravityAmount. The - reverses the right direction so it points left. You can use this same method for the other directions.

First it seems you have some logic error where: If(Gravitronned = true … it needs ==

Second, your gravity is going to start out inversed which may be what you want, but where your going to have trouble is when you switch to normal gravity, and then back, because you will need to add Gravitronned = false in your first if statement. I hope that helps!

Use force over lifetime module, with a constant.