How to stop movement, when mouse is off screen.

I’m hoping there’s someone out there that can help me with a small problem.

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.

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.

Here is the code for my Input Manager.

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 > screenBoundsWidth - boundary) {
            position.x += speed * Time.deltaTime;
        }

        if (Input.mousePosition.x < 0 + boundary) {
            position.x -= speed * Time.deltaTime;
        }

        if (Input.mousePosition.y > screenBoundsHeight - 10) {
            position.y += speed * Time.deltaTime;
        }

        if (Input.mousePosition.y < 0 + boundary) {
            position.y -= speed * Time.deltaTime;
        }   

        Camera.mainCamera.transform.position = position;
    }
}

Thank you for your time.

EDIT

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.

if (Input.mousePosition.x  < screenBoundsWidth && Input.mousePosition.y < screenBoundsHeight) {
    if (Input.mousePosition.x > screenBoundsWidth - boundary) {
        position.x += speed * Time.deltaTime;
    }
}

if (Input.mousePosition.x > 0 && Input.mousePosition.y > 0) {
    if (Input.mousePosition.x < 0 + boundary) {
        position.x -= speed * Time.deltaTime;
    }
}

if (Input.mousePosition.y < screenBoundsHeight && Input.mousePosition.x < screenBoundsWidth) {
    if (Input.mousePosition.y > screenBoundsHeight - 22) {
        position.y += speed * Time.deltaTime;
    }
}

if (Input.mousePosition.y > 0 && Input.mousePosition.x > 0) {
    if (Input.mousePosition.y < 0 + boundary) {
        position.y -= speed * Time.deltaTime;
    }
}

Solution solved by Jacek Przemieniecki over on Stack Overflow.

3 Ideas:

Rect screenRect = new Rect(0,0, Screen.width, Screen.height);
if (!screenRect.Contains(Input.mousePosition))
    return;

The same can be written more verbously as:

float mouseX = Input.MousePosition.x;
float mouseY = Input.MousePosition.y;
float screenX = Screen.width;
float screenY = Screen.height;

if (mouseX < 0 || mouseX > screenX || mouseY < 0 || mouseY > screenY)
    return;

// your Update body

…which is pretty much the same as your “hacky” solution (which is completely valid imho).

Another option is to create 4 Rect objects for each screen border, then check if mouse is inside those rects. Example:

public float boundary = 50;
public float speed = 4;
private Rect bottomBorder;
private Rect topBorder;
private Transform cameraTransform;

private void Start()
{
    cameraTransform = Camera.mainCamera.transform
    bottomBorder = new Rect(0, 0, Screen.width, boundary);
    topBorder = new Rect(0, Screen.height - boundary, Screen.width, boundary);
}

private void Update()
{
    if (topBorder.Contains(Input.mousePosition))
    {
        position.y += speed * Time.deltaTime;
    }

    if (bottomBorder.Contains(Input.mousePosition))
    {
        position.y -= speed * Time.deltaTime;
    }

    cameraTransform.position = position;
}

The tricky part here is that Rect coordinates have Y axis pointing down and Input.mousePosition has Y pointing up… so bottomBorder Rect has to be on the top, and topBorder has to be at the bottom. Left and right borders are not affected.

As of Unity 5.2, Input.mousePosition is now limited by the screen bounds, so it cannot detect when the cursor is outside of the window. However, Input.GetAxis(“Mouse X”) and Input.GetAxis(“Mouse Y”), or whatever you call them, continue to function outside of the window. If Sensitivity is set to 2, they return the cursor’s movement in pixel coordinates. Knowing that, it is easy to find MousePosition.

	Vector2 MousePosition;
	void Update()
	{
		if(Input.mousePosition.y > 0 && Input.mousePosition.y < Screen.height && Input.mousePosition.x > 0 && Input.mousePosition.x < Screen.width)
		{
			MousePosition = Input.mousePosition;
		} else
		{
			MousePosition += new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
		}
	}

Remember: Sensitivity for “Mouse X” and “Mouse Y” must be set to 2.

Here is another reference point to look at http://wiki.unity3d.com/index.php/Custom_2D_Pointer

if mouse position = null?