Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question How to reference Specific Gamepads?

Discussion in 'Input System' started by SuperPox, Jan 8, 2021.

  1. SuperPox

    SuperPox

    Joined:
    Apr 29, 2020
    Posts:
    12
    I'm making a couch co-op game and already have working multiplayer.

    I can not get "gamepad.righttrigger.waspressedthisframe" to work for the 2nd player, works for the 1st.
    My hope was to make unique Gamepad references and call "waspressedthisframe" through them.
    But "Gamepad.current" does not seem to give me a reference to each player's unique gamepad.

    Does anyone know how to reference the unique gamepad attached to a player?
    P.S. this was not helpful (https://docs.unity3d.com/Packages/c...he-gamepad-that-the-player-is-currently-using)

    My code attempt though I doubt it will make sense:
    https://imgur.com/a/4RtR3fT
     
  2. print_helloworld

    print_helloworld

    Joined:
    Nov 14, 2016
    Posts:
    231
    Cheapest way is to do
    Code (CSharp):
    1. Gamepad gamepad = Gamepad.all[index];
     
  3. SuperPox

    SuperPox

    Joined:
    Apr 29, 2020
    Posts:
    12
    ok let me try an index. will check back
     
  4. SuperPox

    SuperPox

    Joined:
    Apr 29, 2020
    Posts:
    12
    Thanks for your help @print_helloworld I tried what you wrote above and the debug still gave me the same read out as before: different index per player, same controller

    I think I need to find all gamepads, add them to a list, and then pair the players with controllers as I iterate the gamepad list. I'll try to see if I can do that tomorrow.
     
  5. SuperPox

    SuperPox

    Joined:
    Apr 29, 2020
    Posts:
    12
    so I seemed to have correctly added my gamepads to a list.
    in the debug the list has 2 objects added to it when I'm playing with two controllers
    however my goal: "myGamepad.buttonEast.wasPressedThisFrame" still does not work
    there should be away to use this function with more than one controller correct?
    @print_helloworld do you have any other ideas?


    void Start()
    {
    playerInput = GetComponent<PlayerInput>();
    var players = FindObjectsOfType<Player>();
    var index = playerInput.playerIndex;
    playerScript = players.FirstOrDefault(m => m.GetPlayerIndex() == index);

    var gamepads = new List<Gamepad>();
    foreach (Gamepad uniqueGamepad in Gamepad.all)
    {
    gamepads.Add(uniqueGamepad);
    Debug.Log(gamepads.Count);
    }

    if (index == 0)
    {
    p1Gamepad = gamepads[0];
    myGamepad = p1Gamepad;
    Debug.Log("My index is: " + index + " // My gamepad is: " + myGamepad);
    }

    if (index == 1)
    {
    p1Gamepad = gamepads[1];
    myGamepad = p1Gamepad;
    Debug.Log("My index is: " + index + " // My gamepad is: " + myGamepad);
    }
    }

    public void OnEast(CallbackContext context)
    {
    if (playerScript != null)
    {
    if (myGamepad == null)
    {
    Debug.Log("no gamepad");
    }
    else
    {
    if (myGamepad.buttonEast.wasPressedThisFrame)
    {
    playerScript.TestPressed();
    }
    if (myGamepad.buttonEast.wasReleasedThisFrame)
    {
    playerScript.TestReleased();
    }
    }
    }
    }