title: How to stop movement, when mouse is off screen
body: <p>I'm hoping there's someone out there that can help me with a small problem.</p>
  
  <p>Currently I have an Input Manager attached to the main camera to allow the user to pan around the map by moving the mouse to the edges of the window, but I've encountered a slight problem which I've tried to fix myself to no avail.</p>
  
  <p>If the mouse goes outside of the window, the panning still happens, which I find irritating when I'm debugging or using other applications. So I am hoping that someone can help me to stop the movement happening when the mouse is outside the game window.</p>
  
  <p>Here is the code for my Input Manager.</p>
  
  <pre><code>using UnityEngine;
  using System.Collections;
  
  public class InputManager : MonoBehaviour {
  
      public Vector3 position = new Vector3(0,0, -10);
      public int boundary = 50;
      public int speed = 4;
  
      private int screenBoundsWidth;
      private int screenBoundsHeight;
  
      // Use this for initialization
      void Start()
      {
          screenBoundsWidth = Screen.width;
          screenBoundsHeight = Screen.height;
  
      }
  
      // Update is called once per frame
      void Update()
      {
          if (Input.mousePosition.x &gt; screenBoundsWidth - boundary) {
              position.x += speed * Time.deltaTime;
          }
  
          if (Input.mousePosition.x &lt; 0 + boundary) {
              position.x -= speed * Time.deltaTime;
          }
  
          if (Input.mousePosition.y &gt; screenBoundsHeight - 10) {
              position.y += speed * Time.deltaTime;
          }
  
          if (Input.mousePosition.y &lt; 0 + boundary) {
              position.y -= speed * Time.deltaTime;
          }   
  
          Camera.mainCamera.transform.position = position;
      }
  }
  </code></pre>
  
  <p>Thank you for your time.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I have come up with a hacky work around, but it still causes the movement to happen in certain locations around the outside of the window. I am hoping someone can come up with a better solution.</p>
  
  <pre><code>if (Input.mousePosition.x  &lt; screenBoundsWidth &amp;&amp; Input.mousePosition.y &lt; screenBoundsHeight) {
      if (Input.mousePosition.x &gt; screenBoundsWidth - boundary) {
          position.x += speed * Time.deltaTime;
      }
  }
  
  if (Input.mousePosition.x &gt; 0 &amp;&amp; Input.mousePosition.y &gt; 0) {
      if (Input.mousePosition.x &lt; 0 + boundary) {
          position.x -= speed * Time.deltaTime;
      }
  }
  
  if (Input.mousePosition.y &lt; screenBoundsHeight &amp;&amp; Input.mousePosition.x &lt; screenBoundsWidth) {
      if (Input.mousePosition.y &gt; screenBoundsHeight - 22) {
          position.y += speed * Time.deltaTime;
      }
  }
  
  if (Input.mousePosition.y &gt; 0 &amp;&amp; Input.mousePosition.x &gt; 0) {
      if (Input.mousePosition.y &lt; 0 + boundary) {
          position.y -= speed * Time.deltaTime;
      }
  }
  </code></pre>