【问题标题】:How to stop movement, when mouse is off screen当鼠标离开屏幕时如何停止移动
【发布时间】:2013-06-18 20:43:37
【问题描述】:

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

目前我有一个连接到主摄像头的输入管理器,允许用户通过将鼠标移动到窗口边缘来平移地图,但我遇到了一个小问题,我试图自己解决无济于事。

如果鼠标移到窗口之外,仍然会发生平移,当我在调试或使用其他应用程序时,我觉得这很烦人。所以我希望有人可以帮助我阻止鼠标在游戏窗口外时发生的移动。

这是我的输入管理器的代码。

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

感谢您的宝贵时间。

编辑

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

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

【问题讨论】:

  • 这是用于 WinForms 的吗?
  • 不幸的是,它适用于 Unity3D,但我使用的是 C#。

标签: c# unity3d


【解决方案1】:

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”解决方案几乎相同(恕我直言,这是完全有效的)。

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

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 必须在顶部在底部。左右边框不受影响。

【讨论】:

  • 感谢您提供如此深入的回答,我将看看是否可以按照您的说法进行操作。再次感谢!
  • 就我个人而言,我选择了您的第一个解决方案,它运行得非常好,完全符合我的要求。谢谢!
【解决方案2】:

由于 Unity 和各种主机操作系统的交互方式,您对鼠标光标的控制有限。 (简而言之,操作系统控制鼠标 Unity 只是读取它)话虽如此,您确实有 一些 选项。 Screen.lockCursor 突然想到。

http://docs.unity3d.com/Documentation/ScriptReference/Screen-lockCursor.html

它不会完全符合您的要求,但它可能是一个很好的起点

【讨论】:

  • 我已经对此进行了研究,对于我正在创建的 RTS 游戏,这并没有达到我想要的效果,我想我可能也给这篇文章提供了错误的标题。跨度>
【解决方案3】:

时间.速度 = 0;

这是你想要的吗?

【讨论】:

    猜你喜欢
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    • 2013-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-28
    • 1970-01-01
    相关资源
    最近更新 更多