移动鼠标而不移动光标

时间:2018-03-18 12:52:10

标签: c# unity3d

我正在尝试使用鼠标在按住键的同时旋转游戏对象来实现自由旋转系统。就像Planet Coaster的物体旋转系统一样。

我目前有一个可行的脚本,该对象围绕与水平鼠标移动相对应的法线轴旋转,但是因为我也将对象移动到鼠标位置,所以在我释放之后对象跳转到鼠标所在的位置旋转键。

有没有办法在仍然获得Input.GetAxis("Mouse X")值时停止实际光标移动,或者将光标移动到坐标以便我可以保存预旋转鼠标位置并将光标设置到该位置轮换完成后?

我找到了一些Unity论坛链接,讨论使用另一个GameObject,其变换链接到鼠标位置(https://answers.unity.com/questions/925711/how-can-i-move-the-mouse-without-moving-the-cursor.html)并将其用作“软件”光标,但其他线程谈论这是一个不好的想法,但它们似乎都已经好几年了,可能已经过时了。

作为参考,我当前的对象旋转代码是:

void Update () 
{
    // uses a raycast to get the mouse position on the terrain
    hitPoint = GetMousePosition(); 

    if (!Input.GetKey(RotationKey))
    {
        // if not holding the rotation key, move the building to the mouse position
        transform.position = hitPoint;
    }
    else
    {
        // rotate the building according to Mouse X position
        var _placementRotationY = -Input.GetAxis("Mouse X") * RotationSpeed * Time.deltaTime;
        transform.Rotate (transform.up, _placementRotationY);
    }

    // rest of method deals with placing object
}

1 个答案:

答案 0 :(得分:0)

Unity的文档讨论了如何锁定光标位置:docs.unity3d.com/ScriptReference/Cursor-lockState.html

锁定光标不会停止鼠标X上的统一输入,这是一个例子:

using UnityEngine;

public class rotateScreen : MonoBehaviour {
    public float rotationSpeed = 360.0f;
    CursorLockMode wantedMode;
    // Use this for initialization
    void Start () {
        wantedMode = Cursor.lockState = CursorLockMode.Locked;
        // Hide cursor when locked
        Cursor.visible = false;
    }

    // Update is called once per frame
    void Update () {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            if(wantedMode == CursorLockMode.Locked)
            {
                wantedMode = Cursor.lockState = CursorLockMode.None;
                // Hide cursor when locked
                Cursor.visible = true;

            }
            else
            {
                wantedMode = Cursor.lockState = CursorLockMode.Locked;
                // Show our cursor when unlocked
                Cursor.visible = false;

            }
        }

        if (wantedMode == CursorLockMode.Locked)
        {
            Vector3 rotation = Vector3.zero;
            rotation.y = Input.GetAxis("Mouse X");

            transform.Rotate(rotation * Time.deltaTime);
        }
    }
}