Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    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

    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
  6. kayusoft

    kayusoft

    Joined:
    Mar 22, 2014
    Posts:
    18
    Rene-Damm's example is good but i think it is old.
    So If you are looking for a solution without using buit-in PlayerInput component and Instantiating, my example code is below:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.InputSystem;
    3. using UnityEngine.InputSystem.Utilities;
    4.  
    5. [RequireComponent(typeof(Rigidbody))]
    6. public class LocalPlayerController : MonoBehaviour
    7. {
    8.     [SerializeField] private float moveSpeed = 5f;
    9.     [SerializeField] private float rotationSpeed = 720f;
    10.     [SerializeField] private PlayerInputType playerInputType = PlayerInputType.KeyboardMouse;
    11.     private ReadOnlyArray<InputDevice> keyboardMouseDevices = new ReadOnlyArray<InputDevice>(new InputDevice[] { Keyboard.current, Mouse.current });
    12.     private ReadOnlyArray<InputDevice> gamepadDevice = new ReadOnlyArray<InputDevice>(new InputDevice[] { Gamepad.current});
    13.  
    14.     private Rigidbody rb;
    15.     private Vector3 movement;
    16.     private LocalPlayerInput input;
    17.  
    18.     void Start()
    19.     {
    20.         rb = GetComponent<Rigidbody>();
    21.         input = new LocalPlayerInput();
    22.         input.Enable();
    23.         input.devices = playerInputType == PlayerInputType.KeyboardMouse ? keyboardMouseDevices : gamepadDevice;
    24.         input.LocalPlayer.Enable();
    25.     }
    26.  
    27.     void Update()
    28.     {
    29.         Vector2 dir = input.LocalPlayer.Movement.ReadValue<Vector2>();
    30.         movement = new Vector3(dir.x, 0.0f, dir.y).normalized;
    31.  
    32.         if (movement != Vector3.zero)
    33.         {
    34.             Quaternion toRotation = Quaternion.LookRotation(movement, Vector3.up);
    35.             transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, rotationSpeed * Time.deltaTime);
    36.         }
    37.     }
    38.  
    39.     void FixedUpdate()
    40.     {
    41.         rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
    42.     }
    43. }
    44.  
    45. public enum PlayerInputType
    46. {
    47.     KeyboardMouse,
    48.     Gamepad
    49. }
    Assign it to your player prefab and duplicate it and change Player Input Type for both of them.

    And here is my Input Actions:
    LocalPlayerInput.png