- Home /
Rigidbody Rotation Problem
I have a rigidbody with wheelcolliders and I am using a spline (Super Splines) to guide the rigidbody round a track. It's not been without problems, fighting with the physics but, I have a solution I am happy with a seems stable. The last issue I am having is with rotation of the rigidbody whilst in the air to do a flip. It can rotate forward or backwards up to 90 degrees and then just flips out. I am controlling the Y axis via a point on the spline, trying to lock the Z axis down to stop any tipping from side to side and then I want to leave the X axis to the physics.
Here are the methods that make this happen,
void HandcarRotation()
{
float rotationX = myTransform.rotation.eulerAngles.x;
float rotationY = splineRotation.eulerAngles.y;
float rotationZ = myTransform.rotation.eulerAngles.z;
myTransform.rotation = Quaternion.Euler(rotationX, rotationY, rotationZ);
}
void SetToSplinePosition()
{
//set the local x velocity to zero to help prevent sliding off the spline
Vector3 localVelocity = myTransform.InverseTransformDirection(myRigidbody.velocity);
localVelocity = new Vector3(0f, localVelocity.y, localVelocity.z);
myRigidbody.velocity = myTransform.TransformDirection(localVelocity);
//set handcar position onto spline
Vector3 position = new Vector3(splinePosition.x, myTransform.position.y, splinePosition.z);
myTransform.position = position;
}
void LockZRotation()
{
//freeze localz rotation
Vector3 localRotation = myTransform.localRotation.eulerAngles;
localRotation = new Vector3(localRotation.x, localRotation.y, 0f);
myTransform.localRotation = Quaternion.Euler(localRotation);
}
void TiltControl()
{
if(!IsGrounded())
{
float tiltDirection;
tiltDirection = Input.acceleration.y;
myRigidbody.AddRelativeTorque(Vector3.left * tiltDirection * tiltForce, ForceMode.Acceleration);
}
}
If I had to guess I would say it's something to do with local and world coordinates but, I'm not sure how to fix it.
Thanks for your help
Answer by DanMarionette · Jan 29, 2012 at 07:19 PM
I have found the solution. After trying lots of things with code I found some info about the X axis having trouble past 87 degrees. I have no idea of the details of what causes this and how it can be prevented but, to solve my problem I have rotated my vehicle 90 degrees on the X axis making X now forward and Z right. This way I can lock X and rotate on Z and get the effect I want. Z doesn't seem to have the same 90 degree limitation X has.
Like I said I don't fully understand why but, I have seen similar questions mentioning the same issues.
Thanks
Answer by Rapport-Noize · Feb 06, 2018 at 03:07 AM
GimbaLock?? you are working with vectors or eulerangles? why not try with quaternion for fix gimbal lock?
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
How to keep rotation from switching between - and + after a 360? 0 Answers
How do i keep a rigidbody upright? Looking for best way to freeze rotation 1 Answer
Freezing Rotation and Joints (Swing / Rope) Physics Issues 1 Answer