Search Unity

Resolved Example of code needed - Using 2 xbox controllers with diffrent prefabs

Discussion in 'Input System' started by struty84, Mar 20, 2022.

  1. struty84

    struty84

    Joined:
    Nov 11, 2018
    Posts:
    1
    Hi all.
    Im making a fun learning game for my boys when each player control 1 character.
    when im working with 1 joystick and 1 keyboard all is working great, but when i want to set up a 2nd xbox controller(instead of keyboard) i just cant get it to work.
    i saw each xbox controller have a diffrent Device ID - is there a code i can use to read it on script?
    Alternatively i tried to use the player Input manager, but i can only use 1 prefab and i want each player to have a diffrent character.

    please advice.
     
  2. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Either switch the two instances in the scene explicitly during
    OnEnable
    :

    Code (CSharp):
    1. player1.SwitchCurrentControlScheme("Gamepad", gamepad1);
    2. player2.SwitchCurrentControlScheme("Gamepad", gamepad2);
    Or instantiate them like this:

    Code (CSharp):
    1. PlayerInput.Instantiate(prefab1, pairWithDevice: gamepad1);
    2. PlayerInput.Instantiate(prefab2, pairWithDevice: gamepad2);
     
    struty84 likes this.