- Home /
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.
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.
Answer by RetepTrun · Jul 18, 2011 at 08:04 PM
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;
}
}
}
Answer by SilverTabby · Jul 07, 2011 at 10:57 PM
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; // :)
thanks @SilverTabby exactly what I was looking for
I need for planetary gravity so if I go to the other site I don't end up falling from the planet :P
now I just have to figure out how to turn my body around the sphere, ...
You might want to place this in FixedUpdate()
, and if you do, remove Time.deltaTime
.
Your answer
Follow this Question
Related Questions
Player in Spaceship 2 Answers
What's the Rigidbody's gravity unit? 1 Answer
Problems making a glider 3 Answers