Search Unity

New Input System - Sometimes Not Registering Mouse Click Released

Discussion in 'Input System' started by coatline, Jun 1, 2022.

  1. coatline

    coatline

    Joined:
    Jul 31, 2019
    Posts:
    17
    Code (CSharp):
    1. bool fireButtonDown;
    2.  
    3. public void OnFire(InputAction.CallbackContext value)
    4. {
    5.         float val = value.ReadValue<float>();
    6.         print(val);
    7.  
    8.         if (value.performed)
    9.         {
    10.             if (fireButtonDown == true) return;
    11.             fireButtonDown = true;
    12.             print("down");
    13.         }
    14.         else if(value.canceled)
    15.         {
    16.             if (fireButtonDown == false) return;
    17.             fireButtonDown = false;
    18.             print("up");
    19.         }
    20. }
    21.  
    22. void Update()
    23. {
    24.       if (fireButtonDown)
    25.              gun.Fire();
    26. }
    About the code:
    - The OnFire function is hooked up to the input action "Fire" via the editor (Invoke Unity Events).
    - The "Fire" action is binded to the Left Button [Mouse].
    - The "Fire" action is of action type "Value" and control type of "Any".
    - Still happens when I changed the action type to "Button".
    - Seems to work more consistently when in play mode on full screen on a separate monitor.

    In play mode:
    - Click the left mouse button with relative quickness.
    - May work more consistently when adding right clicks in with it.

    Without fail, this eventually happens:
    - Mouse button is not down but the gun keeps firing.
    - Not always but on occasion, at the frame of the missed input, there appears to be a frame drop.
    - The debug logs show that there is one extra "down" print than "up" indicating that the OnFire function is not even being entered.
    - Sometimes it takes 5 clicks, sometimes 160 for this to happen

    Afterwards, while I am still shooting (the game thinks that my mouse is still down):
    - I click again and I for this single click, I get the debug console to print "up" but no "down".

    I am using Unity 2022.1.0f1 on Windows 10
    Input System Package Version 1.3.0 - January 05, 2022


    Thank you for your time.
     
    Last edited: Jun 2, 2022
  2. naknuknik

    naknuknik

    Joined:
    Mar 14, 2021
    Posts:
    19
  3. coatline

    coatline

    Joined:
    Jul 31, 2019
    Posts:
    17
  4. naknuknik

    naknuknik

    Joined:
    Mar 14, 2021
    Posts:
    19
    It seems to me like what you described should work, but if it doesnt Id try these:
    - Log the phase of the action every time the callback is called and when the issue repros go over the last logs to see which phases it went through.
    - Remove processor and check if it still happens reliably
    - Set a framerate limit (i think its something like Application.targetFrameRate = 5) to see if this makes it happen more reliably
    - Try to change interaction from default(https://docs.unity3d.com/Packages/c...ual/Interactions.html#predefined-interactions)
    to a press Interaction with behaviour set to press and release, subscribe to its performed and inside that check if the control magnitude is above or below the pressPoint(default is 0.5,but i think u can just get it out of the context)
    Again,take everything I say with a pinch of salt,as im not too experienced and the info I got is just from some mild reading.
    Edit: presspoint default is 0.5 not 5 :D
     
    Last edited: Jun 7, 2022
    coatline likes this.
  5. coatline

    coatline

    Joined:
    Jul 31, 2019
    Posts:
    17
    Thanks a lot for your suggestions. I will test all of these at some point in time when I get the chance.