简体   繁体   English

如何检测鼠标是否在Unity中停止移动

[英]How to detect if Mouse stopped moving in Unity

I want to do a Game like slither.io in Unity but it's hard to detect when the mouse stopped moving, I want the ball to not stop, instead it should move on, worked several hours on this problem but can't quite get it to work. 我想在Unity中做类似splitter.io的游戏,但是很难检测到鼠标何时停止移动,我希望球不停止,而是应该继续前进,在这个问题上工作了几个小时,但还是不能完全解决上班。 I thought if I save where the last known position is I can keep the velocity but I don't know how to implement yet. 我以为如果保存最后一个已知的位置,就可以保持速度,但是我还不知道如何实现。

Thanks in advance! 提前致谢!

This is my Source Code: 这是我的源代码:

private float xMin, xMax, yMin, yMax;
[SerializeField] float constantSpeed = 100f;
[SerializeField] float padding = 5f;
private Rigidbody2D rb2D;
private Vector3 mousePosition;
private Vector2 direction;
private List<Vector3> ListPos = new List<Vector3>();
private Vector3 empty;

// Use this for initialization
void Start () {
    empty = Vector3.zero;
    SetUpMoveBoundaries();
    rb2D = gameObject.GetComponent<Rigidbody2D>();
}

// Update is called once per frame
void Update () { 
    Move();
}

private void SetUpMoveBoundaries()
{
    Camera gameCamera = Camera.main;
    xMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).x + padding;
    xMax = gameCamera.ViewportToWorldPoint(new Vector3(1, 0, 0)).x - padding;
    yMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).y + padding;
    yMax = gameCamera.ViewportToWorldPoint(new Vector3(0, 1, 0)).y - padding;
}

private void Move()
{

    if (Input.mousePosition != empty)
    {
        mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        direction = (mousePosition - transform.position).normalized;
        ListPos.Add(direction);
        rb2D.velocity = new Vector2(direction.x * constantSpeed, direction.y * constantSpeed);
    }

    else
    {
        var last = ListPos.LastOrDefault();
        rb2D.velocity = new Vector2(last.x * constantSpeed, last.y * constantSpeed);
    }
}

Assuming you want it to trigger one of the two functions depending on whether the mouse has moved or not since last frame: 假设您希望它根据自上一帧以来鼠标是否移动来触发两个功能之一:

Vector2 lastMousePosition;
void WhenMouseIsMoving()
{
}
void WhenMouseIsntMoving()
{
}
void Update()
{
 if (Input.mousePosition!=lastMousePosition)
 {
   lastMousePosition=Input.MousePosition;
   WhenMouseIsMoving();
  } else
   WhenMouseIsntMoving();
}

You'll need to add one more bool variable to keep track of whether it has just started or stopped moving; 您需要再添加一个bool变量,以跟踪它是刚刚开始还是停止了移动。

If you want to check for mouse movement without having to keep track of the mouse position in a variable, you can use the GetAxis function from Unity's Input class. 如果要检查鼠标的移动而不必跟踪变量中的鼠标位置,则可以使用Unity的Input类中的GetAxis函数。

To do this you will have to make sure that mouse movement is hooked up to axes in Unity's input manager. 为此,您必须确保将鼠标移动连接到Unity输入管理器中的轴上。 It is normally there by default already, with 'Mouse X' and 'Mouse Y' mapped to the mouse delta for their respective axes. 通常默认情况下它已经存在,“ Mouse X”和“ Mouse Y”已映射到它们各自轴的鼠标增量。

For example: 例如:

// Is true when the mouse has moved
if (Input.GetAxis("Mouse X") != 0 || Input.GetAxis("Mouse Y") != 0)
{
    // Do something with mouse input
}

In your case, it looks like you could do something like this as your move function: 在您的情况下,您似乎可以将以下功能用作移动函数:

Vector2 direction = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
if (direction.magnitude != 0)
{
    // ListPos.Add(direction);
    rb2D.velocity = direction.normalized * constantSpeed;
}

As an unrelated note, your ListPos variable is growing in size potentially every frame without limit. 与此无关, ListPos变量的大小可能每帧无限制地增长。 If you need to keep track of previous positions for reasons other than your attempt at detecting mouse position changes, you should consider how much storage you will need and give it a fixed size replacing the oldest entries, or consider whether or not the values need to be independent or can be merged instead. 如果您出于其他原因而不是试图检测鼠标位置变化的原因而需要跟踪先前的位置,则应考虑需要多少存储空间,并为它固定大小以替换最旧的条目,或者考虑是否需要将这些值是独立的,也可以合并。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM