0

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?

derHugo
  • 83,094
  • 9
  • 75
  • 115
Akuu
  • 1
  • 1
  • There is not enough information in question. What the property `targetGear.RelativeRotation` does, how it is calculated? – Xander Aug 16 '23 at 16:47
  • I calculate RelativeRotation as follows: `public Quaternion RelativeRotation { get => Quaternion.Inverse(_startRotation) * transform.rotation; }` – Akuu Aug 17 '23 at 09:34

1 Answers1

1

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;
derHugo
  • 83,094
  • 9
  • 75
  • 115
  • Thank you for the response. Using SlerpUnclamped to calculate half of the rotation worked correctly, but I couldn't achieve the desired result. If there is a 2-fold difference between the first gear and the second gear, when the first gear completes 360 degrees, the second gear jumps back to 0 degrees after 180 degrees. The reason I used Euler was to calculate beyond 180 degrees, but I couldn't manage it. I think there's a mistake in the logic I've set up. – Akuu Aug 17 '23 at 09:40