x


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) {
        other.attachedRigidbody.AddForce(0,other.attachedRigidbody.mass*-9.81,0);
    }
}

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

more ▼

asked Jul 07 '11 at 10:47 PM

RetepTrun gravatar image

RetepTrun
323 9 14 16

Just attach a ConstantForce component on your objects, set the relative force on those to whatever you want them to be, and rotate the objects based on the normal of the surface that they are currently resting on.

Jul 07 '11 at 10:57 PM Dreamblur
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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

       other.attachedRigidbody.AddForce(cameraRelativeRight * -9.81,ForceMode.Acceleration);
       }
    }


} 



//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;
       }
    }
}
more ▼

answered Jul 18 '11 at 08:04 PM

RetepTrun gravatar image

RetepTrun
323 9 14 16

(comments are locked)
10|3000 characters needed characters left

Use

var gravityVector = Vector3(0, -9.81, 0);
rigidbody.AddForce( gravityVector * Time.deltaTime, ForceMode.Acceleration);

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; // :)

more ▼

answered Jul 07 '11 at 10:57 PM

SilverTabby gravatar image

SilverTabby
1.9k 3 6 18

you want to use mothership.transform.up, not Vector3.up

Jan 18 '12 at 02:01 PM royce3
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1880
x1797
x464
x292
x181

asked: Jul 07 '11 at 10:47 PM

Seen: 1682 times

Last Updated: Jan 18 '12 at 02:01 PM