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