title: Limit Unity Orthographic Camera Panning Movement
body: <p>I have a script that controls an orthographic camera panning movement in Unity, by using middle mouse button. An empty GameObject (e.g. the name is MainCamPivot (please see the picture)) at the center of the world (0,0,0) acts as the parent of the camera. The camera itself has local euler angles of Vector3(45f, 225f, 0f). I can pan/ move the camera nicely in X and Y axes, with this script (attached to the camera):</p>
  
  <pre><code>public float panSpeed = 0.05f;
  
  private Vector3 lastMousePos;
  
  void Start ()
  {
      lastCamPos = transform.position;
  }
  
  void Update()
  {
     if(Input.GetMouseButtonDown(2))
     {
        lastMousePos = Input.mousePosition;
     }
     if(Input.GetMouseButton(2))
     {
        Vector3 touchDeltaPosition = Input.mousePosition - lastMousePos;
        transform.Translate(-touchDeltaPosition.x * panSpeed, -touchDeltaPosition.y * panSpeed, 0);
        lastMousePos = Input.mousePosition;
     }
  
     // To limit the orthographic camera panning movement:
     if(transform.localPosition.x &lt; 10f) transform.localPosition = new Vector3(10f, transform.localPosition.y, 16.3f);
     if(transform.localPosition.x &gt; 23f) transform.localPosition = new Vector3(23f, transform.localPosition.y, 16.3f);
     if(transform.localPosition.y &lt; 15f) transform.localPosition = new Vector3(transform.localPosition.x, 15f, 16.3f);
     if(transform.localPosition.y &gt; 28f) transform.localPosition = new Vector3(transform.localPosition.x, 28f, 16.3f);
  }
  </code></pre>
  
  <p>To limit the camera panning movement, I set some limits for the X and Y positions of the camera. The problem is the Z position value of the camera always changes, so setting a fixed position for the Z position of the camera will make the camera pans/ moves diagonally like the picture below:</p>
  
  <p><img src="https://i.stack.imgur.com/qVh5Z.jpg" alt="Wrong camera movement"></p>
  
  <p>What is the best method to limit an orthographic camera panning movement properly, with scripting? (like the picture below)</p>
  
  <p><img src="https://i.stack.imgur.com/Pxz1W.jpg" alt="Camera panning movement should follow mouse movement"></p>