Changing Gravity Direction in Unity3D

How to change the gravity direction in Unity3D. How to make the objects fly in Unity.

So let’s start with something simple.

We make our object fly up on space bar button click.

We achieve it with this script:

using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class FlyOnSpaceClick : MonoBehaviour {

    void Update() {
        if (Input.GetKeyUp(KeyCode.Space))
        {
            MakeTheObjectsGoUp();
        }
    }

    public void MakeTheObjectsGoUp()
    {
        Physics.gravity = new Vector3(0f, 1f, 0f);
    }
}

When we add this script to our object, than while the game is running and we press the space bar, our objects are going to go up.

We can reverse the gravity when we call this line of code:

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

After some changes our script would look like this:

using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class FlyOnSpaceClick : MonoBehaviour {

    void Update() {
        if (Input.GetKeyUp(KeyCode.Space))
        {
            MakeTheObjectsGoUp();
        }

        if (Input.GetKey("s"))
        {
            MakeTheObjectsGoDown();
        }
    }

    public void MakeTheObjectsGoUp()
    {
        Physics.gravity = new Vector3(0f, 1f, 0f);
    }

    public void MakeTheObjectsGoDown()
    {
        Physics.gravity = new Vector3(0f, -1f, 0f);
    }

}

After we made those changes, when we start our game we can make the objects go up with the “space” button and go down with the “s” button.

But what we can also do is… change the vector of the gravity to the left or right.

using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class FlyOnSpaceClick : MonoBehaviour {

    void Update() {
        if (Input.GetKeyUp(KeyCode.Space))
        {
            MakeTheObjectsGoRight();
        }
    }

    public void MakeTheObjectsGoRight()
    {
        Physics.gravity = new Vector3(-2f, 0f, 0f);
    }
}

Maybe it doesn’t sound cool, but it is cool!

The same thing we can do if we want to make our objects go left.

using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class FlyOnSpaceClick : MonoBehaviour {

    void Update() {
        if (Input.GetKeyUp(KeyCode.Space))
        {
            MakeTheObjectsGoLeft();
        }
    }

    public void MakeTheObjectsGoLeft()
    {
        Physics.gravity = new Vector3(2f, 0f, 0f);
    }
}

So… We got all possible ways to move our game objects.

Let’s implement the gravity to control it with arrow keys.

using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class FlyOnSpaceClick : MonoBehaviour {

    void Update() {
        if (Input.GetKeyUp(KeyCode.UpArrow))
        {
            MakeTheObjectsGoUp();
        }

        if (Input.GetKeyUp(KeyCode.DownArrow))
        {
            MakeTheObjectsGoDown();
        }

        if (Input.GetKeyUp(KeyCode.RightArrow))
        {
            MakeTheObjectsGoRight();
        }

        if (Input.GetKeyUp(KeyCode.LeftArrow))
        {
            MakeTheObjectsGoLeft();
        }
    }

    public void MakeTheObjectsGoUp()
    {
        Physics.gravity = new Vector3(0f, 2f, 0f);
    }
    public void MakeTheObjectsGoDown()
    {
        Physics.gravity = new Vector3(0f, -2f, 0f);
    }
    public void MakeTheObjectsGoRight()
    {
        Physics.gravity = new Vector3(-2f, 0f, 0f);
    }

    public void MakeTheObjectsGoLeft()
    {
        Physics.gravity = new Vector3(2f, 0f, 0f);
    }
}

This way, when we press the arrow keys our objects will float in this direction.

That’s all for today. I hope this trick will help you to develop new game mechanics. It’s pretty simple and can make your game more colorful.

Other tutorials in the Unity series:

Tutorial on How to make the player or Enemy go through the walls in Unity

How to make Enemy follow the player in Unity Tutorial

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.