Getting a specific portion of rotation in Unity

85 Views Asked by At

I'm trying to implement gear physics using Unity, but I've encountered an issue. I've written the following code for one gear to follow another gear.

Quaternion targetRot = Quaternion.Inverse(Quaternion.Euler(targetGear.RelativeRotation.eulerAngles * ratio));
transform.rotation = _startRotation * targetRot;

If the ratio is not greater than 1, it works smoothly, but when the ratio is greater than 1, the object's right axis rotates by 180 degrees.For example.

enter image description here

When I halve the rotation as in the example, I want the larger gear to make a full rotation. How can I achieve this?

1

There are 1 best solutions below

1
derHugo On

Why go through Euler angles (gimbal lock) at all?

Rather directly stick to the Quaternion instead.

Instead of the ratio multiplication you could instead use Quaternion.SlerpUnclamped

// this interpolates between identity ("no change") and your relative rotation
// by doing this unclamped it basically equals multiplying by the ratio
var targetRot = Quaternion.Inverse(Quaternion.SlerpUnclamped(Quaternion.identity, targetGear.RelativeRotation, ratio));
transform.rotation = _startRotation * targetRot;