- Home /
How best to control rigidbody rotation within constraints
I'm working on a very basic arcadey flight game, and I've been working on controlling the torque of the plane, to constrain individual planes to performance characteristics. To this end, I have individual strengths and max rotation speeds for pitch, roll, and yaw. I'm also trying to ensure that the plane stops turning when it's supposed to. The problem I've run into is that with this code, once a certain rotation is hit, basically all control is lost and it begins spinning on it's own. It can sometimes be broken out of, but I am also having trouble with inputs being reversed. Any help would be appreciated. void Torque(float torqueValue, float pitchValue, float yawValue) { body.AddRelativeTorque(Vector3.forward torqueValue rollStrength, ForceMode.Force); body.AddRelativeTorque(Vector3.right pitchValue pitchStrength, ForceMode.Force); body.AddRelativeTorque(Vector3.up yawValue yawStrength, ForceMode.Force);
if (Mathf.Abs(body.angularVelocity.z) > maxRoll)
{
body.AddRelativeTorque(Vector3.forward * -torqueValue * rollStrength, ForceMode.Force);
}
if (Mathf.Abs(body.angularVelocity.x) > maxPitch)
{
body.AddRelativeTorque(Vector3.right * -pitchValue * pitchStrength, ForceMode.Force);
}
if (Mathf.Abs(body.angularVelocity.y) > maxYaw)
{
body.AddRelativeTorque(Vector3.up * -yawValue * yawStrength, ForceMode.Force);
}
if (torqueValue == 0)
{
body.AddRelativeTorque(Vector3.forward * -body.angularVelocity.z, ForceMode.Force);
}
if (pitchValue == 0)
{
body.AddRelativeTorque(Vector3.right * -body.angularVelocity.x, ForceMode.Force);
}
if (yawValue == 0)
{
body.AddRelativeTorque(Vector3.up * -body.angularVelocity.y, ForceMode.Force);
}
}
Your answer
Follow this Question
Related Questions
Rotating a Rigidbody with Physics 1 Answer
How may I observe expected physical interactions while using Rigidbody.MoveRotation()? 1 Answer
Camera and mesh not colliding 1 Answer
drag object including free rotation 0 Answers
How do i keep a rigidbody upright? Looking for best way to freeze rotation 1 Answer