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.

Question How to prevent Touchscreen controls "getting stuck" using the Input System samples example

Discussion in 'Input System' started by zapposh, Sep 7, 2020.

  1. zapposh

    zapposh

    Joined:
    Nov 12, 2016
    Posts:
    117
    For on-screen buttons I'm using the system with pushes a value to an existing binding from the Unity Sample Scenes (for ex pressing the "Left" button on screen registers an OnPointerDown Event which then sends a value of 1 to the Left-Keyboard arrow of the Input Setting Bindings.

    Easy to set up, and functional at first glance, but after a while one of the controls always ends up getting "stuck". Like the player starts firing or running left all the time.

    It's as if after a while (anywhere between 30 and 60 seconds) OnPointerUp or OnPointerExit don't register properly anymore.

    Is there a workaround? Do I have too many controls on-screen? -> 4 buttons

    Code (CSharp):
    1. using UnityEngine.EventSystems;
    2. using UnityEngine.InputSystem.Layouts;
    3. using UnityEngine.InputSystem.OnScreen;
    4.  
    5. public class ScreenButton : OnScreenControl, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler, IPointerEnterHandler
    6. {
    7.     public void OnPointerDown(PointerEventData eventData)
    8.     {
    9.         SendValueToControl(1.0f);
    10.     }
    11.  
    12.     public void OnPointerEnter(PointerEventData eventData)
    13.     {
    14.         SendValueToControl(1.0f);
    15.         eventData.pointerPress = this.gameObject;
    16.     }
    17.  
    18.     public void OnPointerUp(PointerEventData eventData)
    19.     {
    20.         SendValueToControl(0.0f);
    21.     }
    22.  
    23.     public void OnPointerExit(PointerEventData eventData)
    24.     {
    25.         SendValueToControl(0.0f);
    26.         eventData.pointerPress = null;
    27.     }
    28.  
    29.     private void OnDisable()
    30.     {
    31.         SendValueToControl(0.0f);
    32.     }
    33.  
    34.     [InputControl(layout = "Button")]
    35.     public string m_ControlPath;
    36.  
    37.     protected override string controlPathInternal
    38.     {
    39.         get => m_ControlPath;
    40.         set => m_ControlPath = value;
    41.     }
    42. }
     
  2. zapposh

    zapposh

    Joined:
    Nov 12, 2016
    Posts:
    117
    ps.: the eventData.pointerPress = this.gameObject; and eventData.pointerPress = null; were added for testing but made no difference and things still get stuck after a functioning perfectly for a while.
     
  3. nongbenzgames

    nongbenzgames

    Joined:
    Oct 8, 2018
    Posts:
    19
    Same issue, new InputSystem bug. Happens when 2 touch events occur on the same frame. Only one gets dispatched so the other touch event never exits. I've submitted a report weeks ago but I guess they're backlogged bc no response or fix seen :/

    There's a kind of hacky 'fix' by checking if touch count is 0 each frame then resetting all controls. But kind of disheartening when your code base starts filling up with these bandaids and TODOs.
     
  4. zapposh

    zapposh

    Joined:
    Nov 12, 2016
    Posts:
    117
    That's exactly the way I went too, checking for touch count is 0, but besides it being a hack, it does not play as well either.
     
  5. paulstraw

    paulstraw

    Joined:
    Apr 17, 2017
    Posts:
    4
    Hey folks, I came across this thread while struggling with the same issue. According to the release notes, this was a bug in InputSystem, and was fixed in 1.1.0-preview.3. I upgraded my project (just by switching the version out manually in manifest.json), and it seems to be resolved. Hopefully this helps someone else.
     
    Last edited: Feb 20, 2021
    dkimoto and zapposh like this.
  6. RoryScanlan

    RoryScanlan

    Joined:
    Jan 25, 2014
    Posts:
    139
    Yep, upgrading to the preview package using the manifest file seems to have fixed the button getting stuck for me.
     
  7. dkimoto

    dkimoto

    Joined:
    May 31, 2017
    Posts:
    1
    Thank you for this! I spent a long time trying to figure out what I was doing wrong.
     
  8. Spacelab_Games

    Spacelab_Games

    Joined:
    Jun 29, 2017
    Posts:
    62
    Was this issue actually fixed? cause I'm experiencing it now with my on-screen controls :mad:
     
  9. deckercj28

    deckercj28

    Joined:
    Sep 27, 2018
    Posts:
    4
    I'm facing the same issue using the new input system version 1.1.1 but can only see the issue once I build an apk and run it on my phone. It never shows in the editor (using Unity version 2020.3.18f1). Hoping for any fix or workaround anyone has thought of!
     
    Parachute807 likes this.
  10. deckercj28

    deckercj28

    Joined:
    Sep 27, 2018
    Posts:
    4
    Found out how to fix my particular issue (at least for version 1.1.1 of the new input system), but for me there was an issue with the Input Action UI action map not updating to what the newest version's UI action map was. I had updated from one of the older versions of the Input System, and after importing the new version's input system, replacing my UI action map in my custom input action by deleting the UI map from my custom made input action and copying in the UI map from the default input action that comes with the Input System (under Packages -> Input System -> Plugins -> PlayerInput -> DefaultInputActions -> UI action map). No longer after I replaced the UI action map did I have issues with the sticky joystick when pressing other buttons. Hopefully this can expedite a resolution for anyone else, because it took over a week to figure this out!