New Input System not working in WebGL builds.

What’s the status of the new input system in WebGL builds these days?

All i could find was this old thread: New input system doesn't work in WebGL builds

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:

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

public class cubemovernew : MonoBehaviour
{

    private Keyboard kb;
    private Gamepad pad;

    public float speed = 2.0F;

    // Start is called before the first frame update
    void Start()
    {
        kb = Keyboard.current;
        pad = Gamepad.current;
    }

    // Update is called once per frame
    void Update() {

        var x = transform.position.x;

        if (kb != null) {
            x += kb.dKey.ReadValue() * Time.deltaTime * speed;
            x -= kb.aKey.ReadValue() * Time.deltaTime * speed;
        }

        if (pad != null) {
            x += pad.leftStick.ReadValue().x * Time.deltaTime * speed;
        }

        transform.position = new Vector3(x, transform.position.y, transform.position.z);
    }
}

The same thing works with the old input system, so it shouldn’t be a browser issue.

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 :frowning: 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?

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.

Just to make 100% sure, “Active Input Handling” in the Player project settings is set to “Input System (Preview)”?

1 Like

I’ve tried it multiple times with both “Both” and “Input System (Preview)”. Just switched back to Input System and built again. No input.

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.

Done. Thanks for the quick replies!

1 Like

Also having this issue with controllers only. Keyboard mouse is working but no luck on Xbox Controls. Gamepad does work in regular build though.

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:

public class cubemovernew : MonoBehaviour
{
    private Keyboard kb;
    private Gamepad pad;

    public float speed = 2.0F;

    // Start is called before the first frame update
    void Start()
    {
        kb = Keyboard.current; // in WebGL build will always be null on Start()
        pad = Gamepad.current; // in WebGL build will always be null on Start()
    }

    // Update is called once per frame
    void Update()
    {
        var x = transform.position.x;

        if (kb != null)
        {
            x += kb.dKey.ReadValue() * Time.deltaTime * speed;
            x -= kb.aKey.ReadValue() * Time.deltaTime * speed;
        }
        else
        {
            kb = Keyboard.current; // as a workaround: keep assigning the device value until it's not null
        }

        if (pad != null)
        {
            x += pad.leftStick.ReadValue().x * Time.deltaTime * speed;
        }
        else
        {
            pad = Gamepad.current; // as a workaround: keep assigning the device value until it's not null
        }

        transform.position = new Vector3(x, transform.position.y, transform.position.z);
    }
}
3 Likes

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?

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.

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.

public class InputManager : MonoBehaviour
{
    public PlayerInput player1;
    public PlayerInput player2;

    // Start is called before the first frame update
    void Start()
    {
        var p1 = PlayerInput.Instantiate(player1.gameObject, 1, "player1", 1, Keyboard.current);
        p1.transform.position = new Vector3(2, 0, 0);
        var p2 = PlayerInput.Instantiate(player2.gameObject, 2, "player2", 2, Keyboard.current);
        p2.transform.position = new Vector3(-2, 0 ,0);
    }
}

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

        private void Update()
        {
            if (isPlayer2)
            {
                if (Keyboard.current.dKey.wasPressedThisFrame) _transform.position += new Vector3(0, 1, 0);
            }
            else
            {
                if (Keyboard.current.aKey.wasPressedThisFrame) _transform.position += new Vector3(0, 1, 0);
            }
        }

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

    public PlayerInput player1;
    public PlayerInput player2;

    private bool _isInitialized;

    private void Initialize()
    {
        var p1 = PlayerInput.Instantiate(player1.gameObject, 1, "player1", 1, Keyboard.current);
        var p2 = PlayerInput.Instantiate(player2.gameObject, 2, "player2", 2, Keyboard.current);
        _isInitialized = true;
    }

    private void Update()
    {
        if (!_isInitialized && Keyboard.current.spaceKey.wasPressedThisFrame)
        {
            Initialize();
        }
    }

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

4 Likes

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.

1 Like

I can only get the UI to respond to clicks but nothing else with the mouse. Why?

using System.Collections;
using UnityEngine;
using UnityEngine.InputSystem;

public class CameraController : MonoBehaviour
{
    public float movementSpeed = 0.01f;
    public float lookSpeedMultiplier = 0.2f;
    private Keyboard kb;
    private Mouse ms;
    private Transform tf;
    private bool initialized;
   
    private IEnumerator Start()
    {
        tf = transform;

        while (ms == null || kb == null)
        {
            if(kb==null) kb= InputSystem.GetDevice<Keyboard>();
            if(ms==null) ms= InputSystem.GetDevice<Mouse>();
            yield return null;
        }

        initialized = true;
        Debug.Log("camera controls initialized");
    }
   
    private void Update()       
    {
        if (initialized && ms.rightButton.isPressed)
        {
            var horz = (kb.aKey.isPressed ? -1 : 0) + (kb.dKey.isPressed ? 1 : 0);
            var vert = (kb.sKey.isPressed ? -1 : 0) + (kb.wKey.isPressed ? 1 : 0);
            var rise = (kb.qKey.isPressed ? 1 : 0) + (kb.eKey.isPressed ? -1 : 0);
            var roll = (kb.rKey.isPressed ? 1 : 0) + (kb.tKey.isPressed ? -1 : 0);
            //we are going to multiply all movement by 2 if holding down ctrl or shift on keyboard
            tf.position += (tf.right * horz + tf.forward * vert + -tf.up * rise) *
                       (movementSpeed * ((1 + (kb.leftShiftKey.isPressed ? 1 : 0)) *
                        (1 + (kb.leftCtrlKey.isPressed ? 1 : 0))));
       
            var currentPose = ms.delta.ReadValue();
       
            tf.eulerAngles += new Vector3(
                -currentPose.y * lookSpeedMultiplier,
                currentPose.x * lookSpeedMultiplier,
                roll* lookSpeedMultiplier);
        }
    }   
}

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.

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. :slight_smile: 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).

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

Hey there,
For me simply adding
STARTER_ASSETS_PACKAGES_CHECKED
to
Project Settings/Player/Other Settings/Script Compilation

solved the issue.

as seen in: First Person Starter's input system doesn't work on WebGL

1 Like

This worked for me. ty

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.