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 to remember which controller belongs to which player between scenes?

Discussion in 'Input System' started by NPOCrushader, Nov 5, 2019.

  1. NPOCrushader

    NPOCrushader

    Joined:
    Oct 24, 2018
    Posts:
    5
    I have a problem with my local multiplayer brawler game. In my game scene I have a Player Input Manager which I am using to spawn in my different players. However, what I want to know is how can I rememer which controller spawned in which Player prefab? So that when restarting the match (from a different scene than the main game scene), they spawn the correct player designated to the right controller.

    In addition to that I have a problem that sometimes it gets the same deviceID from the two different Xbox One controllers I am using. This is causeing some problems when trying to join new players.

    Thanks in advance!
     
    Qriva and Filip8429 like this.
  2. Jichaels

    Jichaels

    Joined:
    Dec 27, 2018
    Posts:
    237
    What I was doing when I tried local coop, is that I was spawning a "Player" in the lobby (with PLayerInputManager) but I was never destroying them. Keep them through your scenes, and when you go to your "game" scene, instantiate your characters or whatever and link them to your Player. Was working great for me !
     
    Qriva likes this.
  3. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Would recommend the same approach that @Jichaels uses.

    In case you do want to remember a controller, the device ID (as in InputDevice.deviceId) should be unique and stable across one session of the input system. However, if the controller is unplugged and plugged back in, it will have a new device ID. There's no guaranteed way in which a newly plugged in device can be determined to be the same device that we saw before.

    What the input system does internally is just compare InputDeviceDescriptions. If a device is unplugged and then a device with an identical InputDeviceDescription appears, the system decides it's the same device (which may be wrong).

    Two separate gamepads ending up with the same InputDevice.deviceId would be a bug. Would find that one highly surprising, though. Are you sure it was indeed two different InputDevice instances that existed at the same time and had the same InputDevice.deviceId?
     
    Filip8429 likes this.
  4. NPOCrushader

    NPOCrushader

    Joined:
    Oct 24, 2018
    Posts:
    5
    Yes, in the input debuger the input showed up as expected to the correct controller. One controller had the id of 12, the other 17. But when debugging in the editor, it showed 12 for both controllers. However, this only happened occasionally, usually only on the first press of a button after the other controller had been used. Kind of like it were remembering the last controller input on the first new input from the new controller.