# Change the Gravity direction of Object in Unity

Hello programmers, In this article, I will show how to change the Gravity direction of an Object in Unity.

The gaming experience is improved by changing the gravity direction of the game item. Let’s get the construction process underway.

## Physics.Gravity

The built-in physics of the Unity game engine makes it simple to utilize.

Changes all gravity in your Scene by setting this vector.

By including the rigid body component in the GameObject and turning on gravity, all of the game objects may have gravity applied to them.

On the surface of the Earth, the gravitational acceleration is approximately 9.8 m/s/s.

Syntax:

`Physics.gravity = new Vector3(x, y, z);`

## Unity 3D C# Script:

```using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GravityController : MonoBehaviour
{

// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.W))
{
Physics.gravity = new Vector3(0, 9.8f, 0);
}

else if (Input.GetKeyDown(KeyCode.S))
{
Physics.gravity = new Vector3(0, -9.8f, 0);
}

else if (Input.GetKeyDown(KeyCode.A))
{
Physics.gravity = new Vector3(-9.8f, 0, 0);
}

else if (Input.GetKeyDown(KeyCode.D))
{
Physics.gravity = new Vector3(9.8f, 0, 0);
}

}
}```

## Physics2D.Gravity

The built-in 2Dphysics of the Unity game engine makes it simple to utilize.

Changes all 2D gravity in your Scene by setting this vector2.

By including the rigid body component in the GameObject and turning on gravity, all of the game objects may have gravity applied to them.

On the surface of the Earth, the gravitational acceleration is approximately 9.8 m/s/s.

Syntax:

`Physics2D.gravity = new Vector2(x, y);`

## Unity 2D C# Script:

```using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GravityController : MonoBehaviour
{

// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.W))
{
Physics2D.gravity = new Vector2(0, 9.8f);
}

else if (Input.GetKeyDown(KeyCode.S))
{
Physics2D.gravity = new Vector2(0, -9.8f);
}

else if (Input.GetKeyDown(KeyCode.A))
{
Physics2D.gravity = new Vector2(-9.8f, 0);
}

else if (Input.GetKeyDown(KeyCode.D))
{
Physics2D.gravity = new Vector2(9.8f, 0);
}

}
}```

Using the above codes, tapping the W, S, A, and D keys will change the direction of gravity.