Locally applied Gravity

Hello,

I am creating a space sim where my character can move around in a spaceship that is moving around in space.

The ship has an artificial gravity system and so the player is attached to the deck and up is defined by the deck normals.

The ship flies around in a zero G vaccuum (I am ignoring planetary gravity wells for now) and as the ship rotates around its length I do not want any rigid bodies (the player included) inside the ship to fall sideways.

I want them to stay “glued” to the deck of the artificial gravity system.

Can I use the Unity physics engine to implement this or do I need to develop my own physics engine completely?

Thanks in advance for any pointers on this.

Well, usually you just have to apply a force in the negative normal direction of your “deck” to all objects inside the ship. However If the ship itself is a rigidbody it get’s a bit tricky since the “falling” objects inside the ship would push the ship away.

One solution is to have the ship rigidbody a big outer collider which encloses the whole ship and acts as collider for the ship rigidbody. Then you put an kinematic rigidbody inside the ship rigidbody. This kinematic rigidbody is your actual ship geometry. All things inside the ship will collide with the kinematic rigidbody and it’s intern colliders while the outer rigidbody handles collisions with other objects in space.

To define your artificial gravity area you can use some triggers that covers the inside of the ship. Attach a script like this to those triggers:

//C#
using UnityEngine;

public class GravityZone : MonoBehaviour
{
    public float gravity = 9.81f;
    void OnTriggerStay(Collider aOther)
    {
        var rBody = aOther.rigidbody;
        if (rBody != null)
            rBody.AddForce(-transform.up * gravity)
    }
}