Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to use different devices for the individual players with the new input system?

Discussion in 'Input System' started by Mauschelbaer, Sep 19, 2023.

  1. Mauschelbaer

    Mauschelbaer

    Joined:
    Aug 4, 2019
    Posts:
    17
    I don't use the input system via "Player Input Component", but (as described here) with the "Actions Asset" workflow; so I have the wrapper class generated for the actions and reference them via code.

    However: I have two player prefabs spawned via code which then "bind" the actions in their PlayerController script as follows:

    Screenshot 2023-09-19 235541.png

    Code (CSharp):
    1. // PlayerController.cs
    2.  
    3. private void Awake()
    4.     {
    5.         playerInputActions = new PlayerInput();
    6.         playerInputActions.CharacterControls.Move.performed += OnMove;
    7.         playerInputActions.CharacterControls.Move.canceled += OnMove;
    8.         playerInputActions.CharacterControls.Jump.performed += OnJump;
    9.         playerInputActions.CharacterControls.Jump.canceled += OnJump;
    10.  
    11. }
    But now I control both players at the same time with the keyboard inputs. But I want to make sure that player 1 uses "Keyboard" or "Gamepad No. 1" and player 2 uses, for example, "Gamepad No. 2".

    How can I distinguish this in the code?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,304