Search Unity

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

Question UI interface in multiplayer

Discussion in 'Multiplayer' started by cydonia, Jan 23, 2024.

  1. cydonia

    cydonia

    Joined:
    Apr 7, 2013
    Posts:
    8
    This may sound like a dumb question but I cannot find a solid answer. I have a game that is mostly done I'm just trying to implement multiplayer (of which I have no idea what I'm doing). One thing I have no idea how to approach is the UI. The UI elements that show player health, ammo, minimap ECT needs to be set to the player. In single player this is no problem because I just manually link everything in the scene, job done. But in multiplayer I have no idea how to make this work. If the UI is just in the scene and is client side, then when the player dies and re-spawns, the re-spawned player will be un-linked from the UI. That is not idea. I had thought about just including the UI within the player prefab so when a new prefab spawns it already has a linked UI, but I foresee networking issues with that.

    In the single player I got around the AI enemies un-linking when spawning by having a master copy in the scene that is just duplicated, thus maintaining all the appropriate connections. This will likely be the approach I take, have master copies of the various characters all linked to the UI so when a player re-spawns it's just a duplicate of the master copy.

    However I need to ensure that the UI stays client side and doesn't link to other players in the scene.

    I am at a loss here as I don't know how this will work out or what I can do to ensure the UI not only stays linked to the appropriate player, but doesn't interfere with other players and their UIs. Any suggestions are welcome, please keep in mind this is my first time attempting multiplayer so I know very little about it, so suggestions that include knowledge you assume I already have will likely result in me not having a clue what you are talking about. My experience and knowledge is currently just in single player.
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,057
    I would recommend having a look at the most simple demo/sample for the networking solution you picked.
    Every Unity demo has some UI and it is likely best to just check how they do it for multiple players. How you keep track of references, depends on what you show in the UI. Something like player score is (e.g.) independent of a player-character's death.
     
  3. cydonia

    cydonia

    Joined:
    Apr 7, 2013
    Posts:
    8
    I'll look at some examples but my main concern is linking all the UI variables to the player and having that persist after respawning. The player health bar in particular needs to be linked because if a new player spawns and the UI is un linked, the script assumes health is at zero and destroys the player.

    Adding the UI to the prefab would solve this but I'm not sure if that would cause problems with networking.
     
  4. b4guw1x

    b4guw1x

    Joined:
    Apr 16, 2020
    Posts:
    36
    I would recommend you to use RPC's to sync whatever you want. If your UI is not too complex, it could work. For example i have match end screen on my clients. Show match end command is sent by server with player names in order. And each client takes this rpc, fill the match end screen. Whenever some player send request for rematch, i sent another RPC from server to enable rematch icon on this player. If i don't want to sync anything, i just don't send any RPC.