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

How can I assign an input device to a specific player?

Discussion in 'Input System' started by antoniob98, Feb 1, 2020.

  1. antoniob98

    antoniob98

    Joined:
    Jul 5, 2015
    Posts:
    3
    Hello everyone,
    I'm trying the new input system in Unity, it's really amazing. I'd developing a little local multiplayer game and I was wondering, how can I assign a specific input (gamepad, keyboad etc.) to the player?
    I want to thank everyone in advance and sorry for my English.

    Antonio
     
  2. Rene-Damm

    Rene-Damm

    Unity Technologies

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Depends on what your setup is.

    With PlayerInput, either spawn the players with the given devices or switch devices on the existing PlayerInput instances.

    Code (CSharp):
    1. // Spawn players with specific devices.
    2. var p1 = PlayerInput.Instantiate(playerPrefab, controlScheme: "Gamepad", pairWithDevice: Gamepad.all[0]);
    3. var p2 = PlayerInput.Instantiate(playerPrefab, controlScheme: "KeyboardMouse", Keyboard.current, Mouse.current);
    4.  
    5. // Switch devices later.
    6. p1.SwitchCurrentControlScheme("KeyboardMouse", Keyboard.current, Mouse.current);
    With the generated C# wrappers, assign the devices for the player to "devices" property.

    Code (CSharp):
    1. m_Actions = new MyPlayerActions();
    2. m_Actions.devices = new[] { Keyboard.current , Mouse.current };
     
    EDevJogos likes this.
  3. alterego_devstudios

    alterego_devstudios

    Joined:
    Jul 15, 2020
    Posts:
    1
    Rene please could you help me?
    I am triying to assing two different controllers without spawning the players with this part of your code : "p1.SwitchCurrentControlScheme("KeyboardMouse", Keyboard.current, Mouse.current);". The problem came when both controllers are ps4 controllers and both players dont distinguish each controller. If I spawn the players there are not any problem, but I do not want to do this.
    Thank you :).
     
  4. lordofcarrots

    lordofcarrots

    Joined:
    Nov 9, 2021
    Posts:
    4
    I am having the same problem, can anyone help?
     
  5. coatline

    coatline

    Joined:
    Jul 31, 2019
    Posts:
    17