Search Unity

Handling multiple controllers

Discussion in 'Input System' started by Kirory, Sep 3, 2019.

  1. Kirory

    Kirory

    Joined:
    Aug 31, 2019
    Posts:
    2
    Hello Unity community!

    I'm playing around with the new input system and I'm having some trouble figuring out how to either get the ID of the controller that sent a button, or somehow poll individual controllers. Here's what I have so far:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class GameController : MonoBehaviour
    7. {
    8.     //public PlayerInput playerInputPrefab;
    9.     PlayerControls controls;
    10.  
    11.     private List<PlayerInput> playerInputs;
    12.     private int numPlayers = 4;
    13.  
    14.     void Awake(){
    15.       controls = new PlayerControls();
    16.       controls.GamePlay.Direction.performed += directionInput;
    17.       controls.GamePlay.X.performed += xInput;
    18.       controls.Keyboard.Key.performed += ctx => enter();
    19.       Debug.Log("GameController Awake");
    20.  
    21.  
    22.  
    23.       //ignore this, I'm not sure what I was doing here anymore
    24.       playerInputs = new List<PlayerInput>();
    25.       for(int i = 0; i < numPlayers; i++){
    26.         //playerInputs[i] = Instantiate<PlayerInput>(playerInputPrefab);
    27.         //are we pre-Instantiating PlayerInput instances?
    28.       }
    29.     }
    30.     void Update(){
    31.     }
    32.  
    33.     void directionInput(InputAction.CallbackContext ctx){
    34.       Vector2 direction = ctx.ReadValue<Vector2>();
    35.       Debug.Log(direction.x + ", " + direction.y);
    36.         Debug.Log(direction);
    37.     }
    38.     void xInput(InputAction.CallbackContext ctx){
    39.       Debug.Log("X");
    40.     }
    41.     void enter(){
    42.       Debug.Log("Callback from Enter");
    43.     }
    44.  
    45.     public void OnEnable(){
    46.       controls.Enable();
    47.     }
    48.  
    49.     public void OnDisable(){
    50.       controls.Disable();
    51.     }
    52.  
    53. }
    54.  

    So far, any controller will be routed through each callback. Is it possible to somehow get information about the controller itself? I've seen the current documentation here https://docs.unity3d.com/Packages/c...ityEngine.Experimental.Input.InputSystem.html and have had some trouble understanding how to access things. Any ideas?
     
  2. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    When you say you want the "id of controllers" in you callbacks, what are you actually trying to solve?
     
  3. Kirory

    Kirory

    Joined:
    Aug 31, 2019
    Posts:
    2
    Looking back on that one, I'm not actually sure. Even if I did have that I would still need to somehow use it to find which controller sent something. I think it was probably just a desperate attempt to get any information on multiple controllers whatsoever.


    Edit: I misunderstood your question.
    If, during the callback, I could get which controller caused it, I could handle that specific players action. The current code in my original post above has each callback oblivious to number of controllers and therefore all controllers control the same thing.
     
    Last edited: Sep 3, 2019
  4. Ronnie_88

    Ronnie_88

    Joined:
    Jul 11, 2019
    Posts:
    3
    I have a similar issue. I want to do local co op for two game pads and I get the current gamepads connected like this
    Code (CSharp):
    1. gampad = Gamepad.all;
    then putting them in a list I make. But how would I get one to use a different player(Which aren't the same prefab)? because at the moment both controllers control both different prefabs on screen. Your help is appreciated!
     
  5. BindingForceDev

    BindingForceDev

    Joined:
    Aug 13, 2014
    Posts:
    40
    I dunno if this helps, but I used the PlayerInputManager to automatically spawn new prefabs with PlayerInput scripts attached when devices are recognized (an option in the component). On that instantiated prefab I attached a custom script I called PlayerControls that generates its state from its PlayerInput by doing the same thing the OP does in awake. Finally, when I spawn a controllable player gameObject I reference the PlayerControls script I want to control that player. You can use the onDeviceGained/Lost delegates in PlayerInput to execute actions when input is gained or lost. I have found that certain controllers work really well when input is lost or regained, but it seems auto-handling lost/regained inputs and isn't fully robust or complete in the 0.9.6 preview. For instance, my Switch Pro Controller acts rather strangely in general (especially wired or when Steam is running), while my Xbox 360 controllers have no issues regaining and losing input.
     
  6. Ronnie_88

    Ronnie_88

    Joined:
    Jul 11, 2019
    Posts:
    3
    This was very helpful but because it was for a school project I switched back to the old input system. I will have to wait until the input system is more robust but again thank you for trying to help
     
  7. BindingForceDev

    BindingForceDev

    Joined:
    Aug 13, 2014
    Posts:
    40
    Hey, my pleasure! I hope others see this either way as it's definitely a great way to handle local multiplayer with the new system.