How can I move the mouse to a specific position by code?

I use cinemachine to move the camera, however when I unpause the camera rotation it rotates to the mouse’s previous position even though it is locked now, is there any method of solving this by changing the mouse position or by another means without having to write a lot?

I don’t use cinemachine, but when you target standalone, there are several ways to set the hardware cursor position from code. Though maybe it’s just a one-frame delay enable thing. Once the cursor has been locked, it should be moved to the center of the screen.

Personally I once created a Unity project where I actually remembered the UI mouse position and set / reset the cursor when the UI was enabled again. A bit like Garry’s mod does it for the UI. Having the cursor always jump to the center can be a bit annoying when you have to use the UI a lot. On the other hand many games just go with that (like Minecraft).

Thanks for the answer, I saw this post but I found it a bit complicated and long. I would just like something to keep the mouse centered when calling a function that locks the mouse to fix the camera moving to the previous position of the mouse , here is the code for you to see kinda how it works.

private void Awake()
{
    characterController = GetComponent<CharacterController>();
    Cam = Camera.main.transform;
    CursorOff();
}

private void Start()
{
    DialogueEvents.Instance.OnStartDialogue += HandleStartDialogue;
    DialogueEvents.Instance.OnFinishDialog += HandleFinishDialogue;
    canMove = true;
}

private void OnDestroy()
{
    DialogueEvents.Instance.OnStartDialogue -= HandleStartDialogue;
    DialogueEvents.Instance.OnFinishDialog -= HandleFinishDialogue;
}

private void HandleStartDialogue(DialogueData obj) { canMove = false;
CursorOn(); }
private void HandleFinishDialogue() { canMove = true; 
CursorOff(); }

void Update()
{
    if (!canMove)
    {
     entradasJogador = Vector3.zero; 
     verticalVelocity = 0;
     characterController.Move(Vector3.zero); 
     CursorOn();
     return;
    }
    transform.eulerAngles = new Vector3(transform.eulerAngles.x, Cam.eulerAngles.y, transform.eulerAngles.z);
    entradasJogador = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    entradasJogador = transform.TransformDirection(entradasJogador);

    characterController.Move(entradasJogador * Time.deltaTime * VelocityPlayer);
    OnFloor = Physics.CheckSphere(verificador.position, 0.3f, layer);
    if(Input.GetKeyDown(KeyCode.Space) && OnFloor)
    {
        verticalVelocity = Mathf.Sqrt(JumpHeight * -2f * gravity);
    }

    if(OnFloor && verticalVelocity < 0)
    {
        verticalVelocity = -1f;
    }

    verticalVelocity += gravity * Time.deltaTime;
    characterController.Move(new Vector3(0, verticalVelocity, 0) * Time.deltaTime);
    if (Input.GetKeyDown(KeyCode.Q))
    {
         if (!MCursor) CursorOn(); 
         else CursorOff();
    }
}

public void CursorOn()
{
    Cursor.lockState = CursorLockMode.None;
    Cursor.visible = true;
    MCursor = true;
    panTilt.enabled = false;
}
public void CursorOff()
{
    Cursor.lockState = CursorLockMode.Locked;
    Cursor.visible = false;
    MCursor = false;
    panTilt.enabled = true;
}

You didn’t say which version of Cinemachine you’re using, or give any details about your Cinemachine camera setup. An image of its inspector would help.

Generally speaking, with Cinemachine you can disable the CinemachineInputAxisController component. That component is responsible for reading the mouse movement and passing it to the camera. While it is disabled, mouse movement will be ignored.

2 Likes

Thanks, that really helped me fix the issue! :slight_smile: Sorry for not mentioning the version befor it’s my first time using this site to ask for help

1 Like