Search Unity

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

    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:
    65
    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:
    296
    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:
    42
    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:
    345
    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:
    4
    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!
     
  27. amiel_ace

    amiel_ace

    Joined:
    Oct 25, 2014
    Posts:
    18
    so frustrating
    what a waste of time
    How do you even play/make a game if the input itself is so broken
     
  28. doublehitgames

    doublehitgames

    Joined:
    Mar 5, 2016
    Posts:
    97
    Yes, Some problem here with Unity 2022.3 Lastested version.

    So, anyone have any soluction for ScreenStick and Screen Buttons? All broke here
     
  29. amiel_ace

    amiel_ace

    Joined:
    Oct 25, 2014
    Posts:
    18
    This is what I end up using, since the problem for me is just the OnScreenStick and my using PlayerInput for the rest of the OnScreenButtons
     
  30. kshitijv

    kshitijv

    Joined:
    Nov 18, 2021
    Posts:
    9
    After Spending so much time I made this script, I am using the generated c# class for input action and it seem to fix all my issue with it, gonna leave it here, maybe it can help someone.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Temp : MonoBehaviour
    {
    [SerializeField] float lookSpeed = 5;
    [SerializeField] float forceMagnitude = 10;
    [SerializeField] float maxVelocity = 6;

    PlayerInputAction playerInputAction;
    Vector2 movementDirection;
    Vector2 lookDirection;
    Rigidbody rb;

    private void Awake()
    {
    playerInputAction = new PlayerInputAction();
    playerInputAction.Enable();
    }

    private void Update()
    {
    LookAtMouse();
    }

    private void FixedUpdate()
    {
    MovePlayer();
    }

    private void MovePlayer()
    {
    movementDirection = playerInputAction.Player.Move.ReadValue<Vector2>();
    movementDirection.Normalize();

    Vector3 movement = new Vector3(movementDirection.x, 0f, movementDirection.y);
    rb.AddForce(movement * forceMagnitude, ForceMode.Force);
    rb.velocity = Vector3.ClampMagnitude(rb.velocity, maxVelocity);
    }

    private void LookAtMouse()
    {
    lookDirection = playerInputAction.Player.Look.ReadValue<Vector2>();
    lookDirection.Normalize();

    Vector3 direction = new Vector3(lookDirection.x, 0f, lookDirection.y);

    if (direction != Vector3.zero)
    {
    Quaternion lookRotation = Quaternion.LookRotation(direction, Vector3.up);
    transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, lookSpeed * Time.deltaTime);
    }
    }
    }
     
  31. skybee_jp

    skybee_jp

    Joined:
    Jul 4, 2023
    Posts:
    7
    Even if no script is attached, the on-screen stick cannot be dragged with the mouse (it immediately returns to 0,0).

    I saw the script above.
    "PlayerInputAction playerInputAction;"
    This will result in an error.
    what is this?
     
  32. kshitijv

    kshitijv

    Joined:
    Nov 18, 2021
    Posts:
    9
    no this will not result in an error, the PlayerInputAction is a C# script that will generate automatically by clicking on the generate C# Script in the Input Action, and whatever you named your Input Action will be your C# Genrated Script.

    in my case, I named my input action PlayerInputAction so my Generated C# script will be called PlayerInputAction.cs. hope this helps tell me if you have any other confusion related to my code.
     
  33. skybee_jp

    skybee_jp

    Joined:
    Jul 4, 2023
    Posts:
    7
  34. UglyDuckling

    UglyDuckling

    Joined:
    Mar 18, 2015
    Posts:
    10
    Hi guys, I just found this thread, right after posting almost the same question in another one. I think I have the same problem as everyone else. Seems to be a major general problem with how the Player Input works?

    In my simple example I have a jump controlled by a On-Screen Button mapped to the Control Path "Button South (Gamepad). To execute the jump I have a PlayerInput component with a Unity Event. This is set up as in the few samples I have found.

    The problem is that the Player Input scheme keeps changing back and forth between Touch and GamePad when i touch the On-Screen button (you can see the active Control Schema in the Player Input inspector). Sometimes cancelling the jump, and making the On-Screen button unresponsive.

    I tried disabling Auto-Switch, setting the default schema to GamePad, but then the On-Screen button does not trigger at all. For testing I'm using the simulator game view with "Simulate Touch Input from Mouse" enabled in the Input Debugger.

    I have also tried the On-Screen Stick, with the same type of problems. The stick just moves a tiny bit before the control scheme switches, cancelling the movement.

    What would the correct way to setup and configure Action, PlayerInput and On-Screen controls? Seems like my sample should be one of the simplest to setup and use, almost out of the box?
     
  35. skybee_jp

    skybee_jp

    Joined:
    Jul 4, 2023
    Posts:
    7
    The area and position of the stick on the screen,
    I think it would be a good idea to make sure it doesn't go to 0.0.
    Avoid placing it on the bottom left.

    Sorry if I misunderstood.
     
  36. kshitijv

    kshitijv

    Joined:
    Nov 18, 2021
    Posts:
    9
    can you show me your input action setup and the script where you are taking inputs from the player, maybe after seeing that I can find the problem in your case.

    also, I would recommend watching Code Monkey video about the input system


    I am using the Generated C# Class variant of input action without using the player input component on the player, because any other way of using the input system does not solve my problem, hope the video helps somewhat.
     
  37. kshitijv

    kshitijv

    Joined:
    Nov 18, 2021
    Posts:
    9
    here is the issue you using Player Input Component in your project don't use that instead use C# Genrated Class method of taking inputs that way you don't even need to use Player Input Component, by doing this solved my problem. I was also having the same problem as you previous by using Player Input Component and the Broadcast Messages. instead when i use C# Genrated Class method of taking input every issue solve itself.
     
  38. kshitijv

    kshitijv

    Joined:
    Nov 18, 2021
    Posts:
    9
    after seeing all your comments, I also tried out the Player Input Component with the Generated C# Class and it also worked fine without any issues, so now we have 2 options. we can use Generated C# Class with or without the Player Input Component, currently, I am trying out changing the input Schemes within the code according to the different devices and running different input codes according to that, will update when i able to do that.
     
  39. skybee_jp

    skybee_jp

    Joined:
    Jul 4, 2023
    Posts:
    7
    https://drive.google.com/file/d/1Mxu9Fe5o8rai9h_joF4fmY4PtmX-E_u0/view?usp=drive_link
    This is a problem before the script, and the image object to which the "on-screen stick" is attached does not move like a video.
    Therefore, any script will not work.
     
  40. kshitijv

    kshitijv

    Joined:
    Nov 18, 2021
    Posts:
    9
    can you also show me your player input or player movement script, because i think the problem is there in those scripts, if you can take a screenshot or put those scripts on google drive then i can see and if i find any issue related to it, i will try my best to help you
     
  41. skybee_jp

    skybee_jp

    Joined:
    Jul 4, 2023
    Posts:
    7
  42. kshitijv

    kshitijv

    Joined:
    Nov 18, 2021
    Posts:
    9
    First of all please separate your code into different classes and the second thing is I think you are using Joystick Pack Asset in the given code and I did not see any mention of Input Action in your code so if the Joystick Pack Asset is doing the job for you then stick with it otherwise shift to New Input System with the Invoke C Sharp Behaviour, you can see that in the code monkey video around 11:14 mark, and you can use my code for the movement that I have posted in the previous post, let me know if you have any other questions regarding it.
     
  43. skybee_jp

    skybee_jp

    Joined:
    Jul 4, 2023
    Posts:
    7
    https://drive.google.com/file/d/1723ur8Ede83fecRmvc_6mZkAWWJF2Bcg/view?usp=drive_link

    Thank you for your reply.
    Thank you for your efforts to resolve the issue.


    As I said at the beginning, the current movement system was introduced because the "on-screen stick" does not work, so there is no point in changing the submitted script.

    The stick doesn't move like the video shows because of some other bad setting on my part.
    So scripts using "on-screen stick" will not work.

    The cause is something else.
    There's something wrong with me.
     
  44. kshitijv

    kshitijv

    Joined:
    Nov 18, 2021
    Posts:
    9
    that could be it, that the issue is in your input action assets, well that's the part of being a game developer solving your issue by spending hours/days and in some cases even weeks (I have been there). well, all the best for your project and hope you found some solution for your case, just remember don't give up even when things get so hard and you feel like there is no way to solve the issue belive me I have been there, there is always a way.