I am creating a 3D game where the player can push a button, and “position” of gravity will be shifted. To make it any clearer, if I press T in the game, all objects and including the character will move upwards because gravity has been re-positioned so that the ceiling is now the new floor or the level. Same thing with the walls. This is what I have been using so far but it isn’t workng:
private void Start()
{
...
Physics.gravity = new Vector3(0, -1.0f, 0);
}
private void InputManager()
{
if (Input.GetKeyDown("T"))
{
Physics.gravity = new Vector3(0, 1.0f, 0);
}
}
Is there another function I should be using or is the solution more complicated than this? The camera isn’t an issue by the way.
Try this sample code :
using UnityEngine;
using System.Collections;
public class ChangeGravity : MonoBehaviour {
// Use this for initialization
void Start () {
Physics.gravity = new Vector3(0, -1, 0);
GetComponent<Rigidbody> ().drag = .5f;
}
// Update is called once per frame
void Update () {
if(Input.GetKeyUp(KeyCode.UpArrow)) {
Physics.gravity = Vector3.up * 5;
}
else if(Input.GetKeyUp(KeyCode.DownArrow)) {
Physics.gravity = Vector3.down * 5;
}
}
}