Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Mouse.WarpCursorPosition moves cursor to incorrect location

Discussion in 'Input System' started by zali_, Jul 17, 2020.

  1. zali_

    zali_

    Joined:
    Aug 29, 2019
    Posts:
    2
    I'm trying to save the cursor location and move it back when the user right clicks and drags, I have an input action set up for right click, that performs the following code:
    Code (CSharp):
    1. public void OnRotationModifier(InputAction.CallbackContext ctx) {
    2.     var rotate = (int) ctx.ReadValue<float>() == 1;
    3.     if (rotate) {
    4.         _lastMousePos = Mouse.current.position.ReadValue();
    5.         Debug.Log("Rotate " + _lastMousePos);
    6.     }
    7.     else if (_lastMousePos != Vector2.zero) {
    8.         Mouse.current.WarpCursorPosition(_lastMousePos);
    9.         Debug.Log("Unrotate " + _lastMousePos);
    10.         _lastMousePos = Vector2.zero;
    11.     }
    12.  
    13.     Cursor.visible = !rotate;
    14.     _rotating = rotate;
    15. }
    I get the same values printed:
    Code (csharp):
    1. Rotate (734.0, 417.0)
    2. Unrotate (734.0, 417.0)
    Yet the cursor goes to the completely wrong part of the screen almost every time. I can't tell if I'm doing something wrong or if this is a bug. Any help would be appreciated.
     
  2. FullMe7alJacke7

    FullMe7alJacke7

    Joined:
    Dec 17, 2013
    Posts:
    55
    Perhaps adding a normalize processor will help? I was getting wonky results until I normalized it via the input system.

    Also I would say maybe try getting the delta instead of position, as this has helped me in the past.
     
  3. zali_

    zali_

    Joined:
    Aug 29, 2019
    Posts:
    2
    I am using deltas for the movement part, this is just to check whether the player has pressed the button to start rotating, as I wanted it to be customizable instead of just checking for RMB. The rotation part itself works flawlessly, I just don't want to use
    CursorLockMode.Locked
    as it moves the cursor to the center every time.
     
  4. rboerdijk

    rboerdijk

    Joined:
    Aug 4, 2018
    Posts:
    96