Search Unity

Bug WebGL Build MousePosition from Event class has an offset when mouse button is released

Discussion in 'Web' started by vika98z, May 19, 2023.

  1. vika98z

    vika98z

    Joined:
    Jul 5, 2019
    Posts:
    3
    Hello!
    I found that when using OnGui and displaying mouse states, when the mouse button is released, the mouse position before releasing is always shifted by one pixel along the y-axis. This issue is only reproducible in the build, not in the editor.
    I tested in Unity 2022.1.22 with an empty scene with one script:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class TestScript : MonoBehaviour
    4. {
    5.     private Vector2 mPrevPosition;
    6.  
    7.     private void OnGUI()
    8.     {
    9.         CollectInputEvent();
    10.     }
    11.  
    12.     private void CollectInputEvent()
    13.     {
    14.         var evt = Event.current;
    15.  
    16.         if (mPrevPosition != evt.mousePosition)
    17.         {
    18.             mPrevPosition = evt.mousePosition;
    19.             Debug.Log($"--- Event.current Move = {evt.mousePosition}");
    20.         }
    21.  
    22.         if (evt.isMouse)
    23.         {
    24.             if (evt.type == EventType.MouseDown)
    25.             {
    26.                 Debug.Log($"--- Event.current MouseDown = {evt.mousePosition}");
    27.             }
    28.             else if (evt.type == EventType.MouseUp)
    29.             {
    30.                 Debug.Log($"--- Event.current MouseUp = {evt.mousePosition}");
    31.             }
    32.         }
    33.     }
    34. }
    In the attached screenshot, I released the mouse button when it was at position (633, 500), but before releasing it, it moved to (633, 499).
    upload_2023-5-19_9-51-21.png
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,979
    This could also be due to your mouse (faulty or bug in firmware) or the fact that releasing a button on the mouse might ever so slightly move the mouse a little bit. Try with a different mouse, or press the button and keep holding and at the same time tape some thick paper under the mouse so it won't produce any more move events even if you shake it, and then release the button to check if it's still moving by 1px on mouse up.

    It is noteworthy that there is a Move event happening before MouseUp which means the mouse did move, or at least windows reported that it did.

    And to be very sure that the bug is with either the project or Unity, try to reproduce it on another computer.
     
  3. vika98z

    vika98z

    Joined:
    Jul 5, 2019
    Posts:
    3
    Thanks for the answer! I checked the bug on different mice, as well as on the touchpad. I'm sure the mouse doesn't move when the button is released. The bug is also reproduced on another computer in different browsers - Chrome, Opera, Firefox.
     
  4. vika98z

    vika98z

    Joined:
    Jul 5, 2019
    Posts:
    3