Search Unity

How do I get input data more often than one per frame?

Discussion in 'Input System' started by kmedved, Oct 31, 2021.

  1. kmedved

    kmedved

    Joined:
    Aug 18, 2016
    Posts:
    189
    Hi! I wanna to get mouse/touch positions often that one time per frame, but seems that Input System can send data only one time per frame:

    Screenshot 2021-10-31 at 14.34.01.png

    Is there any way to get mouse position frequent than 1 time per frame?

    Set UpdateMode to Process Events Manually in InputSystem settings. Using Unity 2021.2.0f1 and InputSystem 1.1.1, MacOS 11.6.1.
     
    Last edited: Oct 31, 2021
  2. Kogar

    Kogar

    Joined:
    Jun 27, 2013
    Posts:
    80
    If you need faster detection I think you need to use the inputaction asset to get events independently from the update loop.
    https://forum.unity.com/threads/mouse-events-not-firing-new-input-system.1152314/

    Something similar happened to me when i tried to get touchinput in the Update() Loop. After i used an event driven system i haven't found anymore missing events.
    https://forum.unity.com/threads/touchphase-began-doesnt-always-happen.986171/#post-7595884

    Here a few tutorials i found about the new inputsystem and inputaction asset
    https://docs.unity3d.com/Packages/c...l/ActionAssets.html#using-input-action-assets
    https://www.raywenderlich.com/9671886-new-unity-input-system-getting-started
    https://gamedevbeginner.com/input-in-unity-made-easy-complete-guide-to-the-new-system/
     
  3. kmedved

    kmedved

    Joined:
    Aug 18, 2016
    Posts:
    189
    Thanks for your reply!

    My game is running at stable 60 fps in Editor. I'm using input actions from an asset and found that input events frequency depends on action, for example: Mouse.Position returns one time pre frame, Mouse.DeltaTime returns twice per frame. My goal is to get mouse/touch positions in real time (it can be 10 times per update if user moves mouse very quick), but seems that InputSystem.Update() works in another way and invokes events more rarely :(
     
  4. dmytro_at_unity

    dmytro_at_unity

    Unity Technologies

    Joined:
    Feb 12, 2021
    Posts:
    212
    Hi,

    You probably hitting a change I did recently. The problem we were trying to work around is if you plug in a 8khz mouse then at 30 fps it will be 266 mouse events per second. This was too much for the current code and it was taking more time .. which meant fps was dropping .. which meant more events were coming in .. which meant it was taking even more time .. until fps drops to 1-2 .. which is not good indeed.

    And in most applications one usually does something among the lines:
    Code (CSharp):
    1. public class MyScript : MonoBehaviour
    2. {
    3.     void Update()
    4.     {
    5.         SomeValue += Mouse.current.delta;
    6.     }
    7. }
    8.  
    So the values of delta between the frames are completely unused here. There is some case in shooter games where you want delta value when something happened, like if mouse button was pressed, but even then it doesn't mean you want all of them.

    Only applications that would need all deltas are drawing applications, but specifics of them is that they are usually not realtime, you start drawing and brush engine buffers all deltas into a queue and applies them in its own time. Meaning you need to release your stylus every-once-in-a-while so brush engine can catchup with the processing. To see that try drawing with a brush in Krita/Photoshop without releasing stylus, after 20-30 sec you will see it just gets overwhelmed a bit and lag between input and drawing increases.

    Also note that mouse position and mouse deltas are decoupled, mouse deltas are driven by low-level RAWINPUT API, while mouse position is higher level windows cursor API. We can't know exact mouse position for every mouse delta change as Windows is coalescing moves so position changes less often.

    If there is some other case that we haven't cought, we have an option to disable all of this:
    InputSettings.disableRedundantEventsMerging = true;
    this will propagate all data as-is, though with risks of eventual frame stalls.

    Please let me know what is your use case so we can architect next version better.
    Thanks!
     
  5. kmedved

    kmedved

    Joined:
    Aug 18, 2016
    Posts:
    189
    Вітаю! Дякую за розгорнуту відповідь! ;)

    I'm working on Painting Asset and a few users requested drawing lines more smooth when user paints very quickly, so I'm looking into new Input System to get mouse/touch positions more often than 1 per frame. My current implementation works in real-time without delays.
    I also thought about Chaikins-Algorithm or any other to smooth drawing path (this method will be still inferior in accuracy compared to the increased frequency of the input data) or implement delay while drawing as you said above.

    Just tried to set
    but I'm still getting 1-2 mouse positions/delta per frame.
    It would be great if developer could read input data more often, if it is possible, ideally - set additional settings for Dynamic Update option (as reading input frequency).
     
    Last edited: Nov 2, 2021
  6. dmytro_at_unity

    dmytro_at_unity

    Unity Technologies

    Joined:
    Feb 12, 2021
    Posts:
    212
    @kmedved Привіт!

    So on which platform you're trying to do this? I believe on Windows you should be able to get all mouse move events, but I'll need to check for other platforms.
     
  7. kmedved

    kmedved

    Joined:
    Aug 18, 2016
    Posts:
    189
    I'm working on Mac OS 11.6.1, MacBook Pro 16' 2019, using touchpad as mouse.
     
  8. dmytro_at_unity

    dmytro_at_unity

    Unity Technologies

    Joined:
    Feb 12, 2021
    Posts:
    212
    @kmedved I've checked macOS source code, so we report every NSEvent with type NSEventTypeMouseMoved, NSEventTypeLeftMouseDragged, NSEventTypeRightMouseDragged, NSEventTypeOtherMouseDragged as mouse update.

    Are you sure macOS reports mouse data moves more often than you observed?
    Maybe we're using the wrong API here, not sure, maybe GCMouseInput is the way to go.
     
  9. kmedved

    kmedved

    Joined:
    Aug 18, 2016
    Posts:
    189
    Unfortunately, I do not know with what frequency the data is received