Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Assigning users to playerinputs

Discussion in 'Input System' started by PaRsOnIsPhErE, Jan 14, 2020.

  1. PaRsOnIsPhErE

    PaRsOnIsPhErE

    Joined:
    Dec 19, 2018
    Posts:
    19
    Hi all, so there's a problem I can't get my head around. I'm not asking for code, I just want advice and pointing in the right direction.

    I'm making a local multiplayer game, which can play up to 4 players. I have 4 player game objects with player inputs components. All working as planned, no problems! Very pleased! The 4 objects have a sprite renderer each which is rendering a different colour sprite for each player ship. I would like though player 1 (user #0) to always be blue, player 2 (user #1) to always be orange etc...

    I'm not near my PC, so bear with me, as I don't have example code, or diagrams but will try and explain everything the best I can.

    The problem I have come across though is every time the game is run, (application closed and restarted), players (users) are re-assigned to different game objects. Now on my main menu I have 4 sprites representing each object with text rendering above. I currently have the text P1, P2, P3, P4, rendering above each sprite to show which colour will be which player.

    On the menu once I've grabbed the user number I will have an action which just highlights the corresponding colour when a gamepad button is pressed! So each player could press a button and find out what colour they are. User#0 will have a separate action map assigned which also has the menu control actions.

    On the start of each level scene, all I need is too be able to grab the user number from the PlayerInput component, and I can then change the sprite of that object to match the diagram from the menu, that way every user will always be the same colour, regardless of how many times the application is restarted.
    I have tried using the below properties to no avail. I'm sure InputUser stores the user number somewhere that's easy to get, but I can't seem to get it from anything I've tried in the docs.

    InputUser user {get;}
    PlayerInput user {get;}
    Playerindex {get;}

    I'm almost certain it's going to be the InputUser user {get;} but I just couldn't get it it to work with errors saying I can't convert this to this, or when there was no errors, it was just showing 0, like it wasn't receiving any data.

    So to conclude, I just need to be able to grab the current user number from the player input component as say an int, so I can then check it and adjust players, actionmaps etc. Just a nudge towards the direction I need too be heading would be great.

    Thank you all for any advice, reply in advance! I'll buy you a beer if we meet in person!
     
  2. smallg2023

    smallg2023

    Joined:
    Sep 2, 2018
    Posts:
    103
    i didn't understand it either so i just told unity not to destroy the playerinput objects when changing scene and i add/update a scene specific script/object as child(ren) which uses the input for it's own actions as needed.
    this way the order doesn't change so you can 'for loop' through them or you can assign scripts to identify which is which from the beginning etc.

    hopefully someone else can answer with a better understanding of the system soon :)
     
  3. PaRsOnIsPhErE

    PaRsOnIsPhErE

    Joined:
    Dec 19, 2018
    Posts:
    19
    Ahh now that's ingenious! Not 100% sure what you mean by a scene specific script with it's own actions?? So you have a 'script' for every scene?? All of my scenes (levels) are 'self contained' so to speak, so no object or code needs too be consistent throughout, apart from score, when I implement that. As I've noticed, when restarting a scene (essentially restarting all the playerinputs), all the users remain attached to the same PlayerInput, so the same objects, I've yet to try switching to a completely different scene, which was on my list of jobs for tonight. But I know that I have nothing in concrete yet to stop player Input from bouncing it's users around.

    If I was in a rush to release I would probably go down that route, but not in any immediate rush as of yet. But it's only reading a a value I need. I can see it in the debug section of the PlayerInput component, it's so close, but yet so far away.

    Been thinking as well, couldn't you just assign a user to a device?? As devices aren't changing between scenes, they are staying consistent, and I read somewhere that even if a device is disconnected, and then re-connected, it will auto assign back to where it was??

    I've read about performpairwithdevice() but all of this is just making me even more confused...

    Thank you for replying! I hope we both get this sorted soon!
     
  4. smallg2023

    smallg2023

    Joined:
    Sep 2, 2018
    Posts:
    103
    well my scenes are different mini-games so i need different logic for each - i.e. some levels won't require a jump etc but if you are using the same basic controls for all your scenes then yes it's fine to just leave the player objects alone once they are created
    yes this might be a better plan, though you will likely find the player's still get moved around in the hierarchy which is annoying to me as i like to 'for loop' a lot and having the players in order is just nicer but my solution does break if a player disconnects but i was just planning on trying to check for that and making them pause the game till a new device is connected or something...

    either way, until a real tutorial/example is released you can now make some progress as i can confirm my method is working fine for multiple scenes :)
     
  5. PaRsOnIsPhErE

    PaRsOnIsPhErE

    Joined:
    Dec 19, 2018
    Posts:
    19
    Awesome! Thank you! I'll just stop the player objects from being deleted at the moment!

    Fingers crossed for an example / tutorial, nudge in the right direction soon!