鼠标不在屏幕上时如何停止运动 [英] How to stop movement, when mouse is off screen

查看:127
本文介绍了鼠标不在屏幕上时如何停止运动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望那里有人可以帮助我解决一个小问题.

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;
    }
}

谢谢您的时间.

编辑

我想出了一个解决办法,但是仍然会导致移动发生在窗口外部周围的某些位置.我希望有人能提出更好的解决方案.

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;
    }
}

推荐答案

3个想法:

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

同样可以这样写:

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

...与您的"hacky"解决方案(完全有效的恕我直言)几乎相同.

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

另一个选择是为每个屏幕边框创建4个Rect对象,然后检查鼠标是否在这些矩形内.示例:

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;
}

这里最棘手的部分是Rect坐标的Y轴指向下方,而Input.mousePosition的Y轴指向上方...因此bottomBorder Rect必须在顶部,而topBorder必须在顶部在底部.左右边框不受影响.

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.

这篇关于鼠标不在屏幕上时如何停止运动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆