EDIT: Okay, I found out why it is doing this, at least. It’s because a quaternion wraps its eulerAngles to 0-360, so as soon as the camera reaches below 0, it starts counting down from 360, or above 360, counting up from 0. This causes the opposite clamp to come into effect.
I am not quite sure how to avoid this though, and have my clamps respect this wrapping.
I’ve got a problem with my clamping of a quaternion for my camera. The code I have clamps the quaternion to the angle I want, but there is an issue where if I get the character into a certain position outside one of the clamping boundaries, the camera seems to snap into a mirror of its clamped axis, turning the other way rather than trying to look at the point it is focused on.
void CameraFixed() {
if(LookAt) {
targetRot = Quaternion.LookRotation((cameraLookAt.transform.position + LookAtOffset) - Camera.main.transform.position);
ClampCameraAngle(ref targetRot);
Camera.main.transform.rotation = Quaternion.Slerp(Camera.main.transform.rotation, targetRot, Time.deltaTime*LookAtSmoothness);
}
}
(This next code is applied before the quaternion is used in a Slerp.)
void ClampCameraAngle(ref Quaternion targetRotation) {
float rotPosX = Mathf.Clamp(targetRotation.eulerAngles.x, initialRotation.x - MinClamp.x, initialRotation.x + MaxClamp.x);
float rotPosY = Mathf.Clamp(targetRotation.eulerAngles.y, initialRotation.y - MinClamp.y, initialRotation.y + MaxClamp.y);
float rotPosZ = Mathf.Clamp(targetRotation.eulerAngles.z, initialRotation.z - MinClamp.z, initialRotation.z + MaxClamp.z);
targetRotation = Quaternion.Euler(new Vector3(rotPosX, rotPosY, rotPosZ));
}
I can fix this issue by clamping the euler angles after Slerping, but then I lose that elastic smoothness that doesn’t cause the camera to feel it’s hitting a brick wall.
Rotations are not my strong point, so I’d appreciate some help in understanding why this is happening and how I can avoid it.