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

Question Why Mouse.current.position doesn't give the same value as Input.mousePosition?

Discussion in 'Input System' started by Secie, Aug 3, 2023.

  1. Secie

    Secie

    Joined:
    Jul 20, 2019
    Posts:
    3
    I encountered this problem while trying to migrate my camera control code from the old input system to the new input system. So I put it to the test in a new scene. Here's my code for test:
    Code (CSharp):
    1.     private Mouse mouse;
    2.     private Camera cam;
    3.     private void Start()
    4.     {
    5.         cam = Camera.main;
    6.         mouse = Mouse.current;
    7.     }
    8.     private void Update()
    9.     {
    10.         Debug.Log($"<color=cyan>New:</color>{mouse.position.ReadValue()}<color=green>Old</color>{Input.mousePosition}");
    11.     }
    And the output below shows the problem I encountered. Mouse.current.position.ReadValue() doesn't give the same value as Input.mousePosition. Even the change of position is different. (Like the vector2.x below, the new input system changed but the old one didn't)

    Am I being stupid ? Doing something wrong ? Or is it a bug ?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,153
    Just looks like there's some buffering on the new input system's values, meaning they probably will be slightly delayed compared to the old system (one frame difference shouldn't be an issue through).

    What kind of camera stuff are you doing that requires the mouse position anyway?
     
  3. Secie

    Secie

    Joined:
    Jul 20, 2019
    Posts:
    3
    Thanks for reply. Just a normal camera control script. I tried to implement the map drag/pan. My original code looks like this:
    Code (CSharp):
    1.     private Mouse mouse;
    2.     private Camera mainCamera;
    3.     Vector3 lastMousePosition;
    4.     Vector3 mouseMovementDelta;
    5.  
    6.     private void Start()
    7.     {
    8.        mainCamera = Camera.main;
    9.        mouse = Mouse.current;
    10.     }
    11.  
    12.  
    13.     private void CameraMoveByDrag()
    14.     {
    15.        if (mouse.leftButton.wasPressedThisFrame)
    16.        {
    17.            lastMousePosition = mainCamera.ScreenToWorldPoint(Input.mousePosition);
    18.        }
    19.        if (mouse.leftButton.isPressed)
    20.        {
    21.            Vector3 currentMousePostion = mainCamera.ScreenToWorldPoint(Input.mousePosition);
    22.            mouseMovementDelta = currentMousePostion - lastMousePosition;
    23.            transform.position -= mouseMovementDelta;
    24.            lastMousePosition = currentMousePostion;
    25.        }
    26.  
    27.     }
    This caused the camera to swing wildly as I dragged, and the camera would still bounce sideways repeatedly after the drag was over. So I suspected the new input system was to blame. But it seems that I shouldn't use ScreenToWorldPoint to get the move direction since when the mouse's world point changes as camera moves. Luckly, I haveI found a way to fulfill my needs.
    Code (CSharp):
    1.     private void CameraMoveByDrag()
    2.     {
    3.         inputDir = Vector2.zero;
    4.         if (mouse.leftButton.wasPressedThisFrame)
    5.         {
    6.             lastMousePosition = mouse.position.ReadValue();
    7.         }
    8.         if (mouse.leftButton.isPressed)
    9.         {
    10.             mouseMovementDelta = mouse.position.ReadValue() - lastMousePosition;
    11.             inputDir.x = -mouseMovementDelta.x * dragSensitivity;
    12.             inputDir.y = -mouseMovementDelta.y * dragSensitivity;
    13.             lastMousePosition = mouse.position.ReadValue();
    14.             moveDir = transform.up * inputDir.y + transform.right * inputDir.x;
    15.             transform.position += moveDir * moveSpeed * Time.deltaTime;
    16.             KeepCameraInScene();
    17.         }
    18.     }
    Still, I was puzzled and curious about the differences between the old and new input systems.