I’m attempting to create a security camera that tracks the player. However, it can only track the player within a specific pitch, heading, and roll limit, where the range is +/- 180-degrees. The below works as intended when the object’s rotation is {0,0,0}. But when I change the heading, the security camera clamps as though its rotation was still {0,0,0}. How do I uniquely clamp a rotation for each axis and still allow the object to be rotated?
void FixedUpdate()
{
// Where 'Pivot' is a child object of the current object
GameObject target = GameObject.FindGameObjectWithTag("Player");
Vector3 direction = (target.collider.bounds.center - Pivot.transform.position).normalized;
direction = Vector3.RotateTowards(Pivot.transform.forward, direction, Mathf.Deg2Rad * RotationSpeed * Time.fixedDeltaTime, 0f);
Vector3 eulerRotation = Quaternion.LookRotation(direction).eulerAngles;
if (eulerRotation.x > 180f)
{
eulerRotation.x -= 360f;
}
if (eulerRotation.y > 180f)
{
eulerRotation.y -= 360f;
}
if (eulerRotation.z > 180f)
{
eulerRotation.z -= 360f;
}
eulerRotation = new Vector3(
Mathf.Clamp(eulerRotation.x, MinPitch, MaxPitch),
Mathf.Clamp(eulerRotation.y, MinHeading, MaxHeading),
Mathf.Clamp(eulerRotation.z, MinRoll, MaxRoll)
);
Pivot.transform.rotation = Quaternion.Euler(eulerRotation);
}