- Home /
How do I keep rigid bodies from sliding when applying fake gravity based on moving parent?
Ok, so the setup here is a little complex. I'm trying to simulate people and things being inside of a ship moving through space.
To achieve this effect, I've made this temp ship out of cube walls and floors and grouped all of that under a "Ship" object in the hierarchy. I've also placed the player there as well.
I wrote a custom character controller for the player, applying velocity to a RB capsule and then applying fake gravity in the FixedUpdate. I'm doing the same thing to RB boxes I've placed around the ship.
The problem is that the objects and player slide across the floor, indicating that something is off in my transform hierarchy assumptions.
The fake gravity is a force applied along the down (negative up) vector of the ship's transform. I assumed that would just work, but I guess I was wrong.
Here are the relevant code bits.
In the ship script:
void FixedUpdate() {
transform.Rotate(new Vector3(0,0,Time.deltaTime * 10));
Vector3 tempVec;
tempVec = transform.up * -98f;
foreach(Rigidbody rb in rigidBodies)
{
//Gravity!!
if(rb.gameObject.tag != "Player")
{
rb.AddForce(tempVec, ForceMode.VelocityChange);
}
}
}
And in the player script:
void FixedUpdate()
{
rigidbody.velocity = lateralMoveVector*(moveSpeed);
//Gravity!!
rigidbody.AddForce(transform.parent.up * -98 * rigidbody.mass, ForceMode.Force);
}
I've tried adjusting the friction on the child objects, as well as using rigid body physics motion on the parent ship instead of a forced rotate.
The friction had no effect, and using rigid body motion on the parent caused the player and other RB children to completely float away from the floor of the ship. This is a big problem, as I intended on steering the parent with RB physics once this was sorted out.
Answer by meat5000 · Aug 31, 2013 at 12:11 AM
Add Physic materials (some have more friction than others) and increase drag on your RigidBodies.
Little discrepancy in your code, I see -98f in one and -98 in the other gravity. 98 is very high, and one is f and not the other. Probably wont make much difference though.
Maybe declare a variable for it at the start to be consistent.
Also, Forcemode acceleration might be a better model for that kind of Physics? I don't know its up to you.
I've actually tried friction and drag values in the rigidbody and Physic materials with little to no success.