Speculative RotateAround, does it exist?

Hi, I’m looking for a version of “RotateAround” that doesn’t modify the transform itself, but would allow me to get back the Quaternion from applying the “RotateAround” transformation to the game object, without affecting the transform itself.

Basically I’m using my mouse’s X position to rotate the camera, and I want to clamp the rotation between [-maxAngle, maxAngle]. In my code right now, I’m rotating an object, then checking if I rotated it too far, then rotating it back. It’s functionally “correct” but I think there exists a helper function somewhere that makes this easier.

float horizontal = Input.GetAxis ("Mouse X") * rotationSpeed;
float currentlyRotated = Quaternion.Angle(transform.rotation, Quaternion.AngleAxis(0, Vector3.up));

// I only want to apply this transformation if I don't translate too far on the Y axis.
transform.RotateAround (transform.position, Vector3.up, horizontal);

// HACK, for now if rotating by horizontal is too far, reverse the rotation.
float cr2 = Quaternion.Angle(transform.rotation, Quaternion.AngleAxis(0, Vector3.up));
if (cr2 > maxAngle) {
	transform.RotateAround (transform.position, Vector3.up, -horizontal);
}

As you can see, this approach is indirect and has too many branches to be good for performance. It’s also just confusing from my point of view without the comments.

Thanks!

Short answer:

No

Long answer:

RotateAround combines a rotation with a translation (rotate the object around itself, while moving it around a pivot point). So a “RotateAround” that only returns a Quaternion makes no sense, since a Quaternion can only be used for rotations, not translations. That’s why RotateAround is on the Transform class.

Fix: Use a custom hierarchy. Create a hierarchy like so:

Pivot
L Camera

You can set the distance from the camera to your pivot arbitrarily by changing the camera’s localPosition. Rotate the Pivot object and the camera will rotate along with it.

With that in mind, you can now easily clamp your rotation Quaternion before applying it to Pivot