Search Unity

Locking multiple controller 'positions'

Discussion in 'Input System' started by Osteel, Jan 23, 2020.

  1. Osteel

    Osteel

    Joined:
    Jan 17, 2014
    Posts:
    59
    Hey everyone,

    We're currently developing a simple interactive experience which uses 4 joystick inputs. Everything is working great with the new input system and players can join the interactive by pulling the trigger on the joystick.

    However, one thing we're hoping to accomplish is mapping the joystick to a specific player. For example, if only one person is playing, but they chose the third controller, they would show up as player 3 in the game.

    Would anyone have any suggestions how to handle this, or if its even possibly logically?

    Thanks!
     
  2. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Cheap Way

    You can exploit the fact that the system tags numbers onto names to make them unique. For example, you can take a binding to "trigger" on "MyJoystick" (replace that with whatever the layout you are using is), toggle it into text mode (the little "T" button) and edit it to read

    Code (CSharp):
    1. <MyJoystick>*1/trigger
    to only bind to the *second* (first one is 0 which is not actually tagged onto the name) MyJoystick.

    However, you will be dependent on the order in which devices are added.

    Better Way

    You can bind to devices by "role". These are just string tags and each device can have however many you want/need.

    So, you can go and tag the devices at runtime like so

    Code (CSharp):
    1. InputSystem.AddDeviceUsage(joystickForPlayer1, "Player1");
    2. InputSystem.AddDeviceUsage(joystickForPlayer2, "Player2");
    3. InputSystem.AddDeviceUsage(joystickForPlayer3, "Player3");
    4. InputSystem.AddDeviceUsage(joystickForPlayer4, "Player4");
    And in bindings you can drop into text mode (like above) and edit paths like so

    Code (CSharp):
    1. <Joystick>{Player1}/trigger
    If the joystick happens to be a layout you have added, you can also set the "commonUsages" property on the layout. This way, the usages show up in the control picker and you don't have to do the manual text editing dance.

    Code (CSharp):
    1. [InputControlLayout(commonUsages = new[] { "Player1", "Player2", "Player3", "Player4" })]
    2. public class MyJoystick : Joystick
    3. {
    4. //...
    ////EDIT: Just to add that, there's a "CommonDeviceUsages" sample that comes with the input system which demonstrates this feature and pretty much does what the second option here outlines.
     
  3. Osteel

    Osteel

    Joined:
    Jan 17, 2014
    Posts:
    59
    Thank you Rene, super helpful (as always). :D