# Artificial Gravity

I intend to make a game that takes place in space where one can land a craft on a larger “carrier” type ship, walk around on it, or even drive a little car about the decks. I would like to make a more complete artificial gravity system than just turning on “use gravity” for objects within the ship.

The method I am trying to use is to apply a downwards force on any object within a trigger collider. This way, if the carrier does a roll, none of the cargo will fall off into space.

``````function OnTriggerStay (other : Collider) {

if (other.attachedRigidbody) {
}
}
``````

the problem is that whether I use “AddForce” or “AddRelativeForce” things on the ship dont seem to behave realistically.

Use

``````var gravityVector = Vector3(0, -9.81, 0);
``````

The main diffrences are that this does not take into account the mass of the object - just like gravity doesn’t - and the ForceMode.Acceleration will make it behave more like gravity because gravity is an acceleration, not a constant force.

Also putting gravity in it’s own vector makes it easier to modify

`gravityVector = Vector3.up * 9.81; // :)`

If the ship is permanently fixed upright then gravitytype can be used, otherwise use forcetype where landing on a ship which is upside down you wont end up falling to the ceiling

``````var forcetype:boolean;
var gravitytype:boolean;

function OnTriggerStay(other : Collider) {

if(forcetype){
if (other.attachedRigidbody) {

// Calculate the y-axis relative to us
var cameraRelativeRight : Vector3 = transform.TransformDirection (Vector3.up);
// Apply a force relative to the our y-axis

}
}

}

//works if upright

function OnTriggerEnter(other : Collider) {
if(gravitytype){

if(other.attachedRigidbody) {
other.attachedRigidbody.useGravity = true;

}
}
}

function OnTriggerExit(other : Collider){
if(gravitytype){
if (other.attachedRigidbody) {
other.attachedRigidbody.useGravity = false;
}
}
}
``````