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

Best way (if there's any) to get all connected gamepads in webGL

Discussion in 'Input System' started by GlenBourgonjon, Oct 22, 2020.

  1. GlenBourgonjon

    GlenBourgonjon

    Joined:
    May 25, 2015
    Posts:
    42
    Hi!

    I have been struggling with this for some weeks now. We're using the new Input System for connecting gamepads and the keyboard, and then assigning those gamepads to different player objects.

    The goal is that in the start screen, the player can either press SPACE on a keyboard or "button south" on any gamepad connected to the pc. Every time "button south" is pressed on a gamepad that hasn't been assigned to a player yet, or SPACE is pressed for the first time, a new "Player" is created. Player is a struct with information like the connected gamepad/keyboard and customization stuff for that player.
    I have tried so many different things, trying to recognize when a button on a gamepad is pressed so I can assign it to a Player. But I never seem to get it completely working. Right now, I only have about 20% chance of the game actually recognizing when a button on a new gamepad is pressed, so it can be added to the list of players. A lot of times, it seems like Unity is unaware of all of the gamepads connected to the computer (even when pressing a button on a gamepad while the game is in focus). What am I doing wrong?
    Here's the code I'm currently using that is giving me the highest success rate, even though it still happens that gamepads just don't want to connect to the game:

    Code (CSharp):
    1. foreach (Gamepad gamepad in Gamepad.all)
    2. {
    3.     if (gamepad != null && gamepad.buttonSouth.wasPressedThisFrame && !IsGamepadUsed(gamepad))
    4.         Player player = Player.AddPlayer(gamepad);
    5. }
    Please keep in mind, all of this works fine in the editor / a Windows build. I'm only having issues with the webGL build. If anyone knows a solution, I would be extremely grateful.
     
  2. DaRealXDev

    DaRealXDev

    Joined:
    Feb 5, 2021
    Posts:
    13
    I'm making a very similar system for a game but I don't think WebGL has support for controllers.