繁体   English   中英

移动鼠标而不移动光标

[英]Move mouse without moving cursor

我正在尝试使用鼠标在按住键的同时旋转游戏对象来实现自由旋转系统。 非常类似于Planet Coaster的对象旋转系统。

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

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

我找到了一些Unity论坛链接,这些链接讨论了使用另一个GameObject的转换与鼠标位置相关的链接( https://answers.unity.com/questions/925711/how-can-i-move-the-mouse-without-移动最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
}

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

锁定光标不会停止在Mouse 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);
        }
    }
}

暂无
暂无

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

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