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

Struggling to associate player handle with a controller

Discussion in 'Input System' started by Tanoran86, Nov 12, 2016.

  1. Tanoran86

    Tanoran86

    Joined:
    Nov 5, 2015
    Posts:
    2
    Hi there, this should hopefully be a fairly quick question.

    I'm currently building a twin stick shooter that is to support up to four players at once. I have control mapping figured out, but I'm having a difficult time figuring out how to assign a player handle to a single controller, or even finding how to get a list of controllers to assign.

    I know you can say:

    handle.AssignDevice(InputDeviceName, true);

    but without a list of valid controllers I can't find the input device names to assign them. Anyone know the answer?
     
  2. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    Hi. We strongly recommend you have each player press a button to join the game; otherwise you can't really be sure there's 4 players playing just because there's four controllers connected. The multiplayer demo scene included with the system shows an example of this approach.

    Otherwise you can look through all connected devices using the InputSystem.devices property. If you only want gamepads, you can ignore those that are not of type Gamepad.
     
  3. Tanoran86

    Tanoran86

    Joined:
    Nov 5, 2015
    Posts:
    2
    Thanks for the speedy response. I looked through the multiplayer demo and after a lot of experimentation I have managed to get the outcome I wanted. When a player hits the Start button on their gamepad, a playerPrefab is spawned and the controller is assigned to that player and all is good (hooray!). But now I have run into two additional problems.

    Since the controllers available are hard-coded into the prototype, I cannot have more than 2 gamepads connected without getting errors. Is there a way to add additional controllers?

    Secondly, I am also getting the issue described in this post:

    https://forum.unity3d.com/threads/bug-gamepad-triggers-interfere-with-left-analog-x-axis.418574/

    Is there anything I can do to address this? For the record, I'm running Windows 10, Unity version 5.4.1f1. This occurs with my official Xbox 360 gamepad, as well as third party Afterglow Xbox 360 gamepads (which Unity handles identically to the official controller).
     
    Last edited: Nov 21, 2016
  4. templewulf

    templewulf

    Joined:
    Dec 29, 2013
    Posts:
    52
    Are you looking at rolling up any of the boilerplate into single methods?

    For example, the demo code in MultiplayerManager has some good examples for setting up a "join button", but that seems like a big task for a lot of user-devs. I was thinking a subclass of PlayerHandle as a property on PlayerInput that can check for inputs on all controllers without consuming inputs might be helpful.

    Something like:
    Code (CSharp):
    1. void Start()
    2. {
    3.     joinAction.Bind(PlayerInput.globalHandle);
    4. }
    5.  
    6. void Update()
    7. {
    8.     if (joinAction.control.wasJustPressed)
    9.     {
    10.         var handle = PlayerHandleManager.CreateHandleForCurrentlyUsedDevices(PlayerInput.globalHandle);
    11.     }
    12. }
    Plus, I was thinking that designating a "join button" would be best done in the editor; declaring it in a class is unsettlingly close to hard-coding. If you take the global handler a step further and don't require users to bind it, you could reference something akin to `PlayerInput.globalHandle.Actions.JoinButton` when checking in Update() and skip the setup in Start().

    Overall, I love the new direction, and I'm excited to know when we can start putting this into production code. Good work, guys!
     
  5. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    We're open to suggestions! But the entire MultiplayerManager is also meant to be wrapping the boilerplate up in a reusable script. It does NOT specify what join or cancel button to use; if you look at the multiplayer demo, there's fields in the script that have references to controls from the ActionMap dragged in.

    Not sure a static PlayerInput.globalHandle can work; there can be multiple global player handles with different ActionMaps assigned. But you're probably right that there's things we can do to simplify!
     
  6. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    Yay!

    We only implemented two for the prototype. Once the proper new input system is ready it will not have this limit.

    I will reply to that post.