Hi, I’m working on improving my math skills, especially with quaternions. A weird behavior came up that I’m curious about…
I’m trying to rotate one object to be ‘near’ identical rotation of a target object using Unity’s torque physics. (I know these aren’t real world accurate or anything… I’d just like it to get it behaving better.) The code I wrote below works fine if I comment out:
myRigid.AddRelativeTorque (newAxis);
and use this line instead (uncomment):
// myRigid.MoveRotation (Quaternion.AngleAxis (newAngle , newAxis));
but obviously that’s not using the torque feature of physics.
Currently the cube is rotating correctly to the camera’s rotation, and then stopping, like it should, but when I change the camera’s rotation from 0.0, 0.0, 0.0 to ANYTHING else the cube is rocking back and forth and BARELY MOVING AT ALL! (It doesn’t decisively go anywhere.)
How do I fix this? Here is the code I wrote:
private void UsingQuaternions ()
{
Quaternion changeRotation = Quaternion.RotateTowards (myRigid.rotation , Camera.main.transform.rotation , 0.0f); // I've played around with the last value (delta) here, I don't think this is causing my problem.
Vector3 newAxis;
float newAngle;
changeRotation.ToAngleAxis (out newAngle , out newAxis);
newAxis.Normalize ();
float angleBetween = Quaternion.Angle (myRigid.rotation , Camera.main.transform.rotation);
if (angleBetween < 3.0f)
{
// This makes the rotation 'gravitate' towards the target area when it gets close.
myRigid.angularVelocity *= 0.6f;
}
else
{
myRigid.AddRelativeTorque (newAxis);
// myRigid.MoveRotation (Quaternion.AngleAxis (newAngle , newAxis));
}
}
UsingQuaternions () is called once every FixedUpdate (), just to clarify.
I’d appreciate any help with this, thanks!