Creating a Custom Gravity System in Unity

In this tutorial, we'll create a custom gravity system in Unity that allows you to simulate gravity in different directions. This can be useful for creating unique gameplay experiences, such as planetary exploration games or levels where gravity changes dynamically. We’ll also address some common questions related to gravity implementation in Unity.

1. Understanding the Concept of Custom Gravity

In Unity, the default gravity pulls objects downwards along the Y-axis. A custom gravity system allows you to apply gravitational forces in any direction, depending on your game’s requirements. This tutorial will focus on creating a basic setup that lets you define the gravity direction for different objects.

2. Setting Up the Project

Start by creating a new Unity project and setting up a simple scene:

  1. Open Unity and create a new 3D project.
  2. In the Hierarchy, right-click and create a 3D Object > Plane to serve as the ground.
  3. Add a 3D Object > Cube to act as the player or object affected by gravity.

3. Creating the Custom Gravity Script

Next, we will create a script that applies custom gravity to objects:

  1. Right-click in the Project window and select Create > C# Script. Name it CustomGravity.
  2. Attach the script to the Cube object by dragging it from the Project window to the Cube in the Hierarchy.

Open the CustomGravity script and replace its contents with the following code:

using UnityEngine;

public class CustomGravity : MonoBehaviour
{
    public Vector3 gravityDirection = new Vector3(0, -9.81f, 0);
    private Rigidbody rb;

    void Start()
    {
        rb = GetComponent();
        rb.useGravity = false; // Disable default gravity
    }

    void FixedUpdate()
    {
        // Apply custom gravity
        rb.AddForce(gravityDirection, ForceMode.Acceleration);
    }
}

4. Configuring the Rigidbody Component

Now we need to configure the Rigidbody component:

  • Select the Cube object in the Hierarchy.
  • In the Inspector, ensure that a Rigidbody component is attached. If not, click Add Component and search for Rigidbody.
  • Set Use Gravity to false to prevent Unity's default gravity from affecting the object.

5. Testing the Custom Gravity

To test your custom gravity system:

  1. Adjust the gravityDirection variable in the Inspector to change the direction of gravity. For example:
  • To simulate gravity pulling to the right, set gravityDirection to (9.81f, 0, 0).
  • To pull upwards, set it to (0, 9.81f, 0).
  • Press Play and observe how the Cube reacts to the custom gravity.

6. Adding Multiple Objects with Different Gravity Directions

To create a more complex environment with multiple objects having different gravity directions:

  1. Duplicate the Cube object several times and change the gravityDirection for each instance in the Inspector.
  2. Experiment with different gravity settings to see how they interact with each other in the scene.

7. Frequently Asked Questions

Q: Can I have multiple gravity sources acting on the same object?

A: Yes, you can modify the CustomGravity script to include logic that sums up multiple gravity sources. You would need to create a manager that tracks all active gravity sources in the scene and applies their forces accordingly.

Q: How do I apply custom gravity only to certain objects?

A: You can use layers or tags to selectively apply the custom gravity. Modify the script to check for specific tags or layers before applying the gravity force.

Q: How do I reset gravity to the default Unity gravity?

A: To reset the gravity, simply set gravityDirection back to (0, -9.81f, 0) and enable the default Unity gravity by setting rb.useGravity to true.

Conclusion

You have successfully created a custom gravity system in Unity. You can expand this system further by adding features such as changing gravity direction based on player input, implementing gravity wells, or allowing certain objects to float. Experiment with different gravity settings to create unique gameplay experiences.

Links
Unity 6