Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question what is up with values returned for the mouse scrollwheel?

Discussion in 'Input System' started by JoeStrout, Apr 16, 2023.

  1. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I'm using the classic Input Manager system, and have what ought to be a very simple need: find out how many "clicks" the user has input via their scroll wheel. Test code (in Update):

    Code (CSharp):
    1.         float rawValue = Input.GetAxisRaw("Mouse ScrollWheel");
    2.         float smoothValue = Input.GetAxis("Mouse ScrollWheel");
    3.         if (rawValue != 0) Debug.Log($"Mouse ScrollWheel raw: {rawValue}  smoothed: {smoothValue}");
    4.  
    ...where Mouse ScrollWheel is defined in the Input Manager settings as:
    upload_2023-4-16_7-49-17.png

    The result: if I move the scroll wheel a single click, I get a value (raw or smoothed) of 0.1000061 in either direction. That's fine, I can deal with a value of 0.1 per click. But when I scroll a bit faster, the values climb much faster than I would expect. If I scroll it a "few" (probably 3-7) clicks, I get values in the 2-3 range (representing 20 or 30 clicks, if it were linear at 0.1/click). If I go a little faster I can see values over 10 or 20, and I am definitely not inputting hundreds of clicks per frame.

    So it seems like the value returned is some nonlinear function of the number of clicks actually scrolled per frame. I can't find this documented anywhere, nor can I figure out how to make it stop.

    I realize that some folks here will say "Use the new Input System package!" I have tried that for other things, and always found it more difficult to use and no better in the end than the classic system. But if it really is the only way to solve this problem, I'm willing to go there.

    Thanks for any insight!

    P.S. I also just checked Input.mouseScrollDelta, and it's showing the exact same bogus values.
    P.P.S. I also just tried the two-finger gesture on my MacBook Pro trackpad, and it doesn't seem to show the problem; here I see absolute values between 0.05 and maybe 1.8, which seems reasonable. Could this be a problem with the physical mouse itself?
     
    Last edited: Apr 16, 2023
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,273
    Almost everything to do with mouse devices has many moving parts in the bowels of the operating system, outside the control or visibility of the layers above like Unity's runtime. Most mouse drivers include features for "acceleration" which will shape the response of movement and also scrolling, as well as axis inversions and fusion with other devices like trackpads, pens, and the like. Any of these features can cause unpredictable or nonlinear responses.
     
  3. Unifikation

    Unifikation

    Joined:
    Jan 4, 2023
    Posts:
    1,073
    This is a result of the Windows "feature": Accelerated Vertical Scroll, and "Accelerated Scroll" for non-microsoft mice.

    Unity SHOULD have provided access to raw mouse data, by default, for games. You can get at it, but you shouldn't have to be doing this kind of low level stuff in the most popular game engine:

    https://learn.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-rawmouse

    If you want to get really into raw data and polling, you can use the audio thread to call native code to poll at rates far faster than your frame rate and physics updates, set flags, etc. Here's a very good introduction to how to do that:

    https://gist.github.com/luluco250/ac79d72a734295f167851ffdb36d77ee
     
  4. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,704
    I'm getting consistent values:
    See what happens with this:
    Code (csharp):
    1. Debug.Log($"Mouse ScrollWheel: {Mouse.current.scroll.ReadValue().y}");
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    It reports the same.

    Turns out this was indeed something happening with my particular mouse and OS settings. In my case it's macOS rather than Windows, but same thing: the "acceleration" feature sends multiple (up to hundreds of) scroll events per frame when you turn the wheel quickly.

    Thanks everyone for your input!