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.
  2. Dismiss Notice

On Screen Controls not working correctly.

Discussion in 'Input System' started by nyscersul, Apr 18, 2020.

  1. nyscersul

    nyscersul

    Joined:
    Oct 17, 2018
    Posts:
    136
    I have used the On Screen controls, creating both a stick and a few buttons, but i cannot seem to get the sticks to work. The strange thing is that they move, but the input is not actually used, the object doesnt move. The object *does* move and work perfectly if i drag the sticks in the editor on my pc, anyone have any ideas what might be wrong?

    Thanks!

    (Let me know if further information is desired, or what information.)
     
  2. Rene-Damm

    Rene-Damm

    Unity Technologies

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    So it works in the editor but does not work in the player? Which platform?

    And the buttons work but the stick does not? Or none of the controls work?
     
  3. MicheleBunetto

    MicheleBunetto

    Joined:
    Jul 2, 2012
    Posts:
    8
    It also doesn't work, from debugging I can see that the component works, but the Player Input does not receive the event
     
  4. nyscersul

    nyscersul

    Joined:
    Oct 17, 2018
    Posts:
    136
    I got it working in the end... cant remember what i changed tho hehe
     
  5. unity_shawn

    unity_shawn

    Joined:
    May 27, 2020
    Posts:
    3
    I got the same problem! when i use player input, i hardly can drag the onscreen stick and the action hardly be triggered. if I don't use player input, I can drag the controll smoothly, but still have no action be triggerd
     
    AndreIvankio and diogo2010rs like this.
  6. Eldirfar

    Eldirfar

    Joined:
    Feb 9, 2014
    Posts:
    58
    Hmmm, any solution for this? I have the same problem that OnScreenControl (LeftStick [GamePad]) works until I add the PlayerInput script on the scene.

    I found that PlayerInput Auto-Switch was causing jittering and to get it to work in the editor I needed to set manually input.SwitchCurrentControlScheme("Keyboard&Mouse"); to remove all devices.
     
    Last edited: Nov 17, 2020
    Mr_Cliff likes this.
  7. RogueStargun

    RogueStargun

    Joined:
    Aug 5, 2018
    Posts:
    286
    Same problem. I drag the stick on my phone and it registers for about a second then stops. I have to wiggle my finger to get continuous movement
     
  8. rnagy90

    rnagy90

    Joined:
    Oct 6, 2020
    Posts:
    1
    Same problem and sadly the official Input System tutorial's source code doesn't containt the On Screen controllers (and if you add it still doesn't work :() but(!) I have found 2 solutions for this.

    The main problem is with the PlayerInput script, because for some reason it sends "canceled" input event immediately after the "performed" event, so you can create a bunch of if statement, and predictions to check the cancel event is fake, or:

    1.) Instantiate the input action from the action map by yourself and enable/disable it on the correct lifecycle event (OnEnable, OnDisable).
    If it's enabled you can read the value from it in the Update() like in the "old" input system.
    You can check this video, and focus on the assets -> c# script generation and usage.



    2.) The second solution is to obtain the asset itself (e.g. create a serialized field and drag that into it) and get the action mappings and actions from it. After that, you can enable/disable the action and add/remove the correct listeners to it.

    Here is a sample code:

    Code (CSharp):
    1. [SerializedField]
    2. InputActionAsset controls;
    3.  
    4. InputAction movement;
    5. InputAction useAction;
    6. InputAction attackAction;
    7.  
    8. ...
    9.  
    10. void Awake()
    11. {
    12.   var actionMap = controls.FindActionMap("Player");
    13.  
    14.   movement = actionMap.FindAction("Move");
    15.   useAction = actionMap.FindAction("Use");
    16.   attackAction = actionMap.FindAction("Attack");
    17.   specialAction = actionMap.FindAction("Special");
    18. }
    19.  
    20. ...
    21.  
    22. void OnEnable()
    23. {
    24.   movement.Enable();
    25.   useAction.Enable();
    26.   attackAction.Enable();
    27.   specialAction.Enable();
    28.  
    29.   movement.performed += OnMove;
    30.   useAction.performed += OnUse;
    31.   attackAction.performed += OnAttack;
    32.   specialAction.performed += OnSpecial;
    33. }
    34.  
     
    Blankeos and blackmodjo like this.
  9. blackmodjo

    blackmodjo

    Joined:
    May 6, 2014
    Posts:
    14
    I subscribe to the things mentioned above, PlayerInput 1.2.0 doesn't work with On-screen Stick
     
  10. PlayCreek

    PlayCreek

    Joined:
    Jul 8, 2020
    Posts:
    3
    yes, On-screen Stick seems to be broken in 1.1.1 and 1.2.0.
     
  11. avivoren

    avivoren

    Joined:
    Sep 21, 2014
    Posts:
    36
    I wrote a possible fix/replacement for the PlayerInput class, for example if you want to use Move action's event you'll need to make OnMove like I did below
    Code (CSharp):
    1.  [SerializeField] private InputActionAsset inputControls;
    2.     private InputAction[] inputActions;
    3.     private void Awake()
    4.     {
    5.  
    6.         InputActionMap actionMap = inputControls.FindActionMap("Player");
    7.  
    8.         inputActions = actionMap.actions.ToArray();
    9.     }
    10.     private void OnEnable()
    11.     {
    12.         foreach (var action in inputActions)
    13.         {
    14.             action.Enable();
    15.             action.performed += ProcessInput;
    16.             action.canceled += ProcessInput;
    17.  
    18.         }
    19.  
    20.     }
    21.     private void OnDisable()
    22.     {
    23.         foreach (var action in inputActions)
    24.         {
    25.             action.Disable();
    26.             action.performed -= ProcessInput;
    27.             action.canceled -= ProcessInput;
    28.  
    29.         }
    30.     }
    31.     private void ProcessInput(CallbackContext callbackContext)
    32.     {
    33.         SendMessage($"On{callbackContext.action.name}", callbackContext, SendMessageOptions.DontRequireReceiver);
    34.     }
    35.     private void OnMove(CallbackContext callbackContext)
    36.     {
    37.         Vector2 currentInput = callbackContext.ReadValue<Vector2>();
    38.  
    39.     }
     
    Last edited: Dec 18, 2021
  12. Navlyc

    Navlyc

    Joined:
    May 21, 2021
    Posts:
    1
    The auto-switch on PlayerInput can cause jittering.To avoid this, there are 2 solutions (I use the second one) :
    - disable auto-switch and select manually ControlScheme playerInput.SwitchCurrentControlScheme(new InputDevice[]{ inputDevice })
    - enable auto-switch but edit ControlScheme (top left button in the InputActionAsset editor) and add Touchscreen device as optionnal
     
    Justinus, saltd and Marc-Saubion like this.
  13. blackmodjo

    blackmodjo

    Joined:
    May 6, 2014
    Posts:
    14
    It is also broken on 1.3.0
     
    Stanley88 and BinaryRavine like this.
  14. BinaryRavine

    BinaryRavine

    Joined:
    Oct 30, 2021
    Posts:
    6
    blackmodjo likes this.
  15. mrm83

    mrm83

    Joined:
    Nov 29, 2014
    Posts:
    343
    the cancelled event is fired when the joystick moves, so it moves, and then it cancels outs.
     
    Stanley88 likes this.
  16. Rouliboudin

    Rouliboudin

    Joined:
    Jan 26, 2022
    Posts:
    3
    still waiting for a fix lol
     
  17. andyblem

    andyblem

    Joined:
    Nov 11, 2014
    Posts:
    26
    After investing so much time in the new input system only to find out it needs some hacks to work on mobile. I will gladly revert back to the old input system or look some third party option.
     
  18. fcorbel

    fcorbel

    Joined:
    Oct 27, 2016
    Posts:
    7
    Hi there !
    I just figured out that I had no EventSystem in the scene. Therefore the On-Screen Stick cannot work. I also had to replace the StandaloneInputModule with the new input system version (Unity kindly offer a button to do that)

    upload_2023-2-3_20-55-12.png

    I hope it can help someone !
     

    Attached Files:

  19. GabrielDPeterson

    GabrielDPeterson

    Joined:
    Jan 21, 2018
    Posts:
    3
    Still an issue in Input System 1.5.0
     
  20. cmdexecutor

    cmdexecutor

    Joined:
    Sep 30, 2014
    Posts:
    21
    Not sure is its’s the same case, anyways might be related to the update type. Change InputSystem update type to manual and invoke InputSystem.current.Update() (don’t remember the exact syntax) within a MonoBehaviour Update()
     
  21. whyatme

    whyatme

    Joined:
    Apr 4, 2021
    Posts:
    20
  22. whyatme

    whyatme

    Joined:
    Apr 4, 2021
    Posts:
    20
    The only workaround I've come up with in hrs of trying to get this to work:

    Code (CSharp):
    1.  
    2.     public void Start()
    3.     {
    4.         EnhancedTouchSupport.Enable();
    5.         playerInput.actions["Move"].performed += HandleMove;
    6.         playerInput.actions["Move"].canceled += HandleMove;
    7.     }
    8.  
    9.     public void HandleMove(InputAction.CallbackContext input)
    10.     {
    11.         if (playerInput.currentControlScheme != "Gamepad")
    12.         {
    13.             return;
    14.         }
    15.         var dir = input.ReadValue<Vector2>();
    16.         OnActionMove.Invoke(dir);
    17.     }
    18.  
    Basically just ignore inputs from other control schemes for these actions.

    Except that when I simulate it on my Android using the Unity Remote app, the final "cancel" event is never called for the Gamepad control scheme. So annoying how inconsistent and buggy this whole thing is.
     
  23. whyatme

    whyatme

    Joined:
    Apr 4, 2021
    Posts:
    20
    Hey all I reported this as a bug with a minimal reproducible project. Hopefully Unity will get back on it.

    In the meantime, the best workaround that I've found is to create a custom On-Screen Stick component.

    In my case I just copied Unity's component, but removed the parts where it uses the broken buggy Input system.

    The idea is that it is a drop-in replacement for the Unity built-in component, with the only difference being that you subscribe to events directly rather than doing it through the Player Input system.

    For example:

    Code (CSharp):
    1.  
    2.     public OnScreenStickCustom movementStick;
    3.  
    4.  
    5.     public void Start()
    6.     {
    7.         movementStick.onJoystickMove.AddListener(OnStickMove);
    8.         movementStick.onJoystickRelease.AddListener(OnStickRelease);
    9.     }
    10.  
    11.     public void OnStickMove(Vector2 direction)
    12.     {
    13.         Debug.Log(direction);
    14.     }
    15.  
    16.     public void OnStickRelease()
    17.     {
    18.          Debug.Log("STOP");
    19.     }
    20.  
    File attached with the custom component.
     

    Attached Files:

  24. Oedsdb

    Oedsdb

    Joined:
    Nov 1, 2020
    Posts:
    4
    Hi all. I believe I have figured out the problem and have your temporary solution! I believe that the Player Input component subscribes to the input events on awake/start/onenable. However, it does this before your on-screen stick is 'enabled'. Therefore, the playerinput does not 'listen' to the on-screen stick.

    A custom fix is simply disabling and re-enabling your player input script. You can manually try this in the editor, and if everything else is set up correctly, it should work!
     
    Justinus likes this.
  25. punkundermyskin

    punkundermyskin

    Joined:
    Aug 10, 2023
    Posts:
    1
    thank you so much
    it actually works!!
     
  26. whyatme

    whyatme

    Joined:
    Apr 4, 2021
    Posts:
    20
    Sure enough Unity reviewed my report and confirmed that this IS a bug on their end. https://issuetracker.unity3d.com/is...ered-every-frame-when-the-move-action-is-used

    So yeah this is completely broken for who knows how long. Leaving this here for anyone that finds it useful, meanwhile I however have since moved on to Godot and am loving it!
     
    XiangAloha likes this.