Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

New Input System not working in WebGL builds.

Discussion in 'Input System' started by pleksus, Apr 17, 2020.

  1. pleksus

    pleksus

    Joined:
    Feb 18, 2013
    Posts:
    4
    What's the status of the new input system in WebGL builds these days?

    All i could find was this old thread:https://forum.unity.com/threads/new-input-system-doesnt-work-in-webgl-builds.672565/

    Can't seem to get it to work with the simplest of implementations.

    This is what I'm trying to build with Unity 2019.3.10f1, InputSystem 1.0.0.preview7:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.InputSystem;
    6.  
    7. public class cubemovernew : MonoBehaviour
    8. {
    9.  
    10.     private Keyboard kb;
    11.     private Gamepad pad;
    12.  
    13.     public float speed = 2.0F;
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         kb = Keyboard.current;
    19.         pad = Gamepad.current;
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update() {
    24.  
    25.         var x = transform.position.x;
    26.  
    27.         if (kb != null) {
    28.             x += kb.dKey.ReadValue() * Time.deltaTime * speed;
    29.             x -= kb.aKey.ReadValue() * Time.deltaTime * speed;
    30.         }
    31.  
    32.         if (pad != null) {
    33.             x += pad.leftStick.ReadValue().x * Time.deltaTime * speed;
    34.         }
    35.  
    36.         transform.position = new Vector3(x, transform.position.y, transform.position.z);
    37.     }
    38. }
    39.  
    The same thing works with the old input system, so it shouldn't be a browser issue.
     
    Last edited: Apr 17, 2020
  2. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    What's happening or not happening in particular?

    Gamepad support on WebGL is crazy spotty ATM. Basically, we only support the "standard" mapping for the WebGL gamepad API (as specified by the standard). However, few browser+gamepad+OS combinations end up giving you that "standard" mapping. Which means... *actual* working gamepad support on WebGL is more miss than hit :( Bit of a dilemma for us. If we take the PS4 DualShock controller, for example. Supporting that across several different platforms and their APIs is already a pain. But when you throw the additional variable of browser+platform into the mix, the permutations get pretty crazy. And that's just one controller. (IMO the WebGL gamepad API is a failure... but that's just my personal opinion)

    However, that said... keyboard is expected to work just fine. Are you not seeing any input coming through?

    What's the platform and browser?
     
  3. pleksus

    pleksus

    Joined:
    Feb 18, 2013
    Posts:
    4
    The thing is, absolutely nothing is happening. No reaction to input from keyboard or gamepad. I was expecting trouble with the gamepad after reading on this a bit, but I can't get any inputs to work from the keyboard either.

    Platform: Windows 10 with latest updates, latest Chrome, tried latest Firefox too. Xbox One controller connected via Bluetooth.
     
  4. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Just to make 100% sure, "Active Input Handling" in the Player project settings is set to "Input System (Preview)"?
     
    irinaha likes this.
  5. pleksus

    pleksus

    Joined:
    Feb 18, 2013
    Posts:
    4
    I've tried it multiple times with both "Both" and "Input System (Preview)". Just switched back to Input System and built again. No input.
     
  6. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Thanks for confirming. Could you file a ticket with the Unity bug reporter? Think it would be good for our QA to have a look. Definitely sounds broken.
     
  7. pleksus

    pleksus

    Joined:
    Feb 18, 2013
    Posts:
    4
    Done. Thanks for the quick replies!
     
    Rene-Damm likes this.
  8. toomasio

    toomasio

    Joined:
    Nov 19, 2013
    Posts:
    198
    Also having this issue with controllers only. Keyboard mouse is working but no luck on Xbox Controls. Gamepad does work in regular build though.
     
  9. unity_s71iKGwGOJobYg

    unity_s71iKGwGOJobYg

    Unity Technologies

    Joined:
    Aug 1, 2019
    Posts:
    1
    So after some investigation, it seems that the problem occurs because in WebGL build Input System cannot retrieve the device values in Start() method. As a workaround you could keep assigning the device value in the Update() until it's not null:
    Code (CSharp):
    1. public class cubemovernew : MonoBehaviour
    2. {
    3.     private Keyboard kb;
    4.     private Gamepad pad;
    5.  
    6.     public float speed = 2.0F;
    7.  
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         kb = Keyboard.current; // in WebGL build will always be null on Start()
    12.         pad = Gamepad.current; // in WebGL build will always be null on Start()
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         var x = transform.position.x;
    19.  
    20.         if (kb != null)
    21.         {
    22.             x += kb.dKey.ReadValue() * Time.deltaTime * speed;
    23.             x -= kb.aKey.ReadValue() * Time.deltaTime * speed;
    24.         }
    25.         else
    26.         {
    27.             kb = Keyboard.current; // as a workaround: keep assigning the device value until it's not null
    28.         }
    29.  
    30.         if (pad != null)
    31.         {
    32.             x += pad.leftStick.ReadValue().x * Time.deltaTime * speed;
    33.         }
    34.         else
    35.         {
    36.             pad = Gamepad.current; // as a workaround: keep assigning the device value until it's not null
    37.         }
    38.  
    39.         transform.position = new Vector3(x, transform.position.y, transform.position.z);
    40.     }
    41. }
     
    ristophonics, dstuck and pleksus like this.
  10. therobby3

    therobby3

    Joined:
    Jan 30, 2019
    Posts:
    131
    Just posting here, it seems that keyboard input using the new input system is not being picked up in Microsoft Edge WebGL builds. On chrome it works fine, but no response in Edge. Anyone else have this issue?
     
  11. i9mobile

    i9mobile

    Joined:
    Aug 8, 2013
    Posts:
    54
    Did you check this information?
    Note that browsers may only allow access to available input devices once the user has interacted with the device while the content has focus. This is a security measure to prevent using the connected devices for browser fingerprinting purposes. For this reason, you should make sure to instruct the user to click a button on their devices before checking Input.GetJoystickNames()) for connected devices.

    https://docs.unity3d.com/Manual/webgl-input.html
     
  12. Rocktavious

    Rocktavious

    Joined:
    May 10, 2017
    Posts:
    44
    So I was recently testing out the new input system for a game jame using 2019.4.11f1 and Input System 1.0.0 (2019.4 verified)

    I am making a local coop so i required the keyboard to be bound to 2 users as well as round robin any gamepads detected.

    I tried using PlayerInput and PlayerInputManager systems and they worked out well but when i compiled and deployed a WebGL build nothing worked. Here is how i was trying to do it.

    Code (CSharp):
    1. public class InputManager : MonoBehaviour
    2. {
    3.     public PlayerInput player1;
    4.     public PlayerInput player2;
    5.  
    6.     // Start is called before the first frame update
    7.     void Start()
    8.     {
    9.         var p1 = PlayerInput.Instantiate(player1.gameObject, 1, "player1", 1, Keyboard.current);
    10.         p1.transform.position = new Vector3(2, 0, 0);
    11.         var p2 = PlayerInput.Instantiate(player2.gameObject, 2, "player2", 2, Keyboard.current);
    12.         p2.transform.position = new Vector3(-2, 0 ,0);
    13.     }
    14. }

    After some intensive digging i found that using PlayerInput with the InputActionAsset was the problem and that if i polled for input it would work like so

    Code (CSharp):
    1.         private void Update()
    2.         {
    3.             if (isPlayer2)
    4.             {
    5.                 if (Keyboard.current.dKey.wasPressedThisFrame) _transform.position += new Vector3(0, 1, 0);
    6.             }
    7.             else
    8.             {
    9.                 if (Keyboard.current.aKey.wasPressedThisFrame) _transform.position += new Vector3(0, 1, 0);
    10.             }
    11.         }

    So guessing that it was a problem with binding the devices at startup i decided to add 1 precursor action of requring the user to press "something" when they were ready and then instantiate the PlayerInput objects like so


    Code (CSharp):
    1.     public PlayerInput player1;
    2.     public PlayerInput player2;
    3.  
    4.     private bool _isInitialized;
    5.  
    6.     private void Initialize()
    7.     {
    8.         var p1 = PlayerInput.Instantiate(player1.gameObject, 1, "player1", 1, Keyboard.current);
    9.         var p2 = PlayerInput.Instantiate(player2.gameObject, 2, "player2", 2, Keyboard.current);
    10.         _isInitialized = true;
    11.     }
    12.  
    13.     private void Update()
    14.     {
    15.         if (!_isInitialized && Keyboard.current.spaceKey.wasPressedThisFrame)
    16.         {
    17.             Initialize();
    18.         }
    19.     }
    Low and Behold this worked (but obviously it doesn't bind the gamepads - but i know how to solve that by listening for unbound devices)

    I'm posting this here incase anyone stumbles upon this and has the same problem with input not working in their WebGL build. It seems with WebGL there is a bug if you bind your PlayerInput to the Keyboard.current at startup and it needs to be done "delayed". Cheers
     
  13. VRKid

    VRKid

    Joined:
    Jul 1, 2016
    Posts:
    42
    same problem, worked on this all day, need to submit this assignment! Bang doesn't work. Compiles fine in Unity... hardly works in WebGL. The OLD system works though. Common guys first the bones, now this.
     
    AronTD likes this.
  14. Nyanpas

    Nyanpas

    Joined:
    Dec 29, 2016
    Posts:
    406
    I can only get the UI to respond to clicks but nothing else with the mouse. Why?
     
  15. Chopium

    Chopium

    Joined:
    Jun 15, 2015
    Posts:
    19
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.InputSystem;
    4.  
    5. public class CameraController : MonoBehaviour
    6. {
    7.     public float movementSpeed = 0.01f;
    8.     public float lookSpeedMultiplier = 0.2f;
    9.     private Keyboard kb;
    10.     private Mouse ms;
    11.     private Transform tf;
    12.     private bool initialized;
    13.    
    14.     private IEnumerator Start()
    15.     {
    16.         tf = transform;
    17.  
    18.         while (ms == null || kb == null)
    19.         {
    20.             if(kb==null) kb= InputSystem.GetDevice<Keyboard>();
    21.             if(ms==null) ms= InputSystem.GetDevice<Mouse>();
    22.             yield return null;
    23.         }
    24.  
    25.         initialized = true;
    26.         Debug.Log("camera controls initialized");
    27.     }
    28.    
    29.     private void Update()      
    30.     {
    31.         if (initialized && ms.rightButton.isPressed)
    32.         {
    33.             var horz = (kb.aKey.isPressed ? -1 : 0) + (kb.dKey.isPressed ? 1 : 0);
    34.             var vert = (kb.sKey.isPressed ? -1 : 0) + (kb.wKey.isPressed ? 1 : 0);
    35.             var rise = (kb.qKey.isPressed ? 1 : 0) + (kb.eKey.isPressed ? -1 : 0);
    36.             var roll = (kb.rKey.isPressed ? 1 : 0) + (kb.tKey.isPressed ? -1 : 0);
    37.             //we are going to multiply all movement by 2 if holding down ctrl or shift on keyboard
    38.             tf.position += (tf.right * horz + tf.forward * vert + -tf.up * rise) *
    39.                        (movementSpeed * ((1 + (kb.leftShiftKey.isPressed ? 1 : 0)) *
    40.                         (1 + (kb.leftCtrlKey.isPressed ? 1 : 0))));
    41.        
    42.             var currentPose = ms.delta.ReadValue();
    43.        
    44.             tf.eulerAngles += new Vector3(
    45.                 -currentPose.y * lookSpeedMultiplier,
    46.                 currentPose.x * lookSpeedMultiplier,
    47.                 roll* lookSpeedMultiplier);
    48.         }
    49.     }  
    50. }
    51.  
    Here's a camera controller that should work with the new input system. Attach to a camera to test. You can change void Start() to IEnumerator Start() to run it as a coroutine and let it wait until both mouse and keyboard are confirmed.
     
  16. Rhydonal

    Rhydonal

    Joined:
    Aug 9, 2017
    Posts:
    1
    Thank you! This was incredibly helpful. I opted for a coroutine to wait 30 frames after Start before executing my code that assigns the keyboard to two users. Worked perfectly. :) I'd image the same thing can be done for other devices and users, and it probably works after waiting just 1 frame (I went with 30 just to be safe).
     
  17. HAMSOFT

    HAMSOFT

    Joined:
    May 31, 2020
    Posts:
    32
    I´m having a similar issue, I´m ussing bolt for my game, but when I maje the build and uploaded to itch.io it doesn´t read the mouse inputs, so it never starts
     
  18. andrlz

    andrlz

    Joined:
    Mar 15, 2021
    Posts:
    6
    hacktron7 likes this.
  19. hacktron7

    hacktron7

    Joined:
    Apr 11, 2023
    Posts:
    1
  20. NaomiZhang

    NaomiZhang

    Joined:
    Aug 1, 2023
    Posts:
    1
    Make sure the playMovement script class name matches the script name. If they don't match, the keyboard still works with other builds except WebGL.
     
    Last edited: Aug 4, 2023
  21. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    1,080
    Hmm, old input system used to seamlessly map Input.mousePosition synonymous with mouse and touch... but new input seems to break this - is there a fix?