Search Unity

Resolved Get Device Attached To Player Input

Discussion in 'Input System' started by Cubester127, May 28, 2020.

  1. Cubester127

    Cubester127

    Joined:
    May 20, 2018
    Posts:
    4
    I am creating a local multiplayer game where I spawn players (with a player input component) using the input manager. I was wondering how I could access the device attached to the player input component of each player.

    For instance, in my case, I would like to get the device, check if it is a ps4 gamepad and set the lightbar of that specific controller to be the player's color. I tried using DualShockGamepad.current but that only gets a single controller.

    Is there any way I can do this in the new Input System?
     
  2. Snowdrama

    Snowdrama

    Joined:
    May 10, 2014
    Posts:
    28
    I think you could probably cast the device from the device list from PlayerInput

    DualShockGamepad ds4 = (DualShockGamepad)GetComponent<PlayerInput>().devices[0];


    though you might have to do some checks to make sure that it's actually a DS4 controller as I assume this will error if the device is say an Xbox controller.
     
  3. Cubester127

    Cubester127

    Joined:
    May 20, 2018
    Posts:
    4
    Thanks, it works.

    For anyone interested, here is the code
    Code (CSharp):
    1. var device = playerInput.devices[0];
    2.  
    3. if (device.GetType().ToString() == "UnityEngine.InputSystem.DualShock.DualShock4GamepadHID")
    4. {
    5.     DualShockGamepad ds4 = (DualShockGamepad)device;
    6.     ds4.SetLightBarColor(playerController.GetComponent<Renderer>().material.color * colorIntensity);
    7. }