简体   繁体   中英

Move mouse without moving cursor

I am trying to implement a free rotation system using the mouse to rotate a gameobject while holding down a key. Much like Planet Coaster's object rotation system.

I currently have a script that works, the object is rotated around the normal axis corresponding to horizontal mouse movement, however because I am also moving the object to the mouse location, the object jumps to where the mouse is after I release the rotation key.

Is there a way to either stop the actual cursor from moving while still getting a Input.GetAxis("Mouse X") value, or, move the cursor to coordinates so I can save the pre-rotation mouse position and set the cursor to that position when rotation is finished?

I have found some Unity forum links that talk about using another GameObject whose transform is linked to the mouse position ( https://answers.unity.com/questions/925711/how-can-i-move-the-mouse-without-moving-the-cursor.html ) and using it as a "software" cursor, but other threads that talk about this being a bad idea, however they all seem to be several years old and possibly out of date.

For reference, my current object rotation code is:

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's documentation talks about locking the cursor position here: docs.unity3d.com/ScriptReference/Cursor-lockState.html

Locking the cursor does not stop unity's input on Mouse X here is an example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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