title: How to properly clamp 360 degrees overlap?
body: <p>I have a camera script where if my character is rotating, the camera rotates too. However, the camera can also be rotated my the mouse pointer. This movement is restricted by a specific set of given degrees, say 30 (maximumX). So the range of camera movement controlled by the mouse should be between 0 and 60 then which is equivalent to -30 and 30 degrees.</p>
  
  <p>If the character rotates a full 360 degrees, then the maxX value overlaps 360 and has to go back to 0. While min may remain something like 300. This creates an issue where the camera will "snap" and not preserve its previous rotation properly.</p>
  
  <pre><code>var newMaxX = ClampAngle(_target.localEulerAngles.y + maximumX, -360, 360);
  var newMinX = ClampAngle(_target.localEulerAngles.y - maximumX, -360, 360);
  if(newMinX &gt; newMaxX)
  {
      var tmp = newMaxX;
      newMaxX = newMinX;
      newMinX = tmp;
  }
  
  if (axes == RotationAxes.MouseXAndY) {
      rotationX += Input.GetAxis("Mouse X") * sensitivityX;
      rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
  
      rotationX = ClampAngle (rotationX, newMinX, newMaxX);
      rotationY = ClampAngle (rotationY, minimumY, maximumY);
  
      var xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
      var yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
  
      cameraTransform.localRotation = originalRotation * xQuaternion * yQuaternion;
  }
  else if (axes == RotationAxes.MouseX) 
  {
      rotationX += Input.GetAxis("Mouse X") * sensitivityX;
      rotationX = ClampAngle (rotationX, newMinX, newMaxX);
  
      xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
      cameraTransform.localRotation = originalRotation * xQuaternion;
  }
  else 
  {
      rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
      rotationY = ClampAngle (rotationY, minimumY, maximumY);
  
      yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
      cameraTransform.localRotation = originalRotation * yQuaternion;
  }
  </code></pre>
  
  <p>Here's the ClampAngle function:</p>
  
  <pre><code>static function ClampAngle (angle : float, min : float, max : float) : float
  {
      if (angle &lt; -360.0)
          angle += 360.0;
      if (angle &gt; 360.0)
          angle -= 360.0;
      return Mathf.Clamp (angle, min, max);
  }
  </code></pre>
  
  <p>Any recommendations?</p>