0
votes

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
}
1
if you googled unity locking the cursor: docs.unity3d.com/ScriptReference/Cursor-lockState.html - AresCaelum
@Eddge I'm aware of that method, however that doesn't really answer the question of moving the cursor to a position after locking it. - Steve
your question doesnt state you need to move the cursor after locking it, it states "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?" You don't need to do that if you want to rotate the screen and lock the mouse. - AresCaelum
@Eddge I'm not rotating the screen, I'm rotating an object that is to be placed in the world. Anything that involves the cursor being in a different position after rotating won't work because the object will jump to the cursor position after releasing the rotate key. - Steve
alright so the object you are rotating, when you hold that button down it isn't suppose to move, so why don't you have that object check the cursor state to see if it is locked, and if it is, it doesnt move? - AresCaelum

1 Answers

0
votes

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