Search Unity

Players in different scenes

Discussion in 'Netcode for GameObjects' started by makeshiftwings, Apr 11, 2022.

  1. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Is there any built in way to have a server/host handle players being in different scenes? If there's not a built in way, is there a good workaround? I was considering loading multiple scenes additively and spatially offsetting them from each other far enough away that the physics and visuals shouldn't affect each other but I feel like there's probably a smarter way to do it.
     
  2. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    664
    Untick Enable Scene Management on the Network Manager component for this. I have the server sitting in its own scene and handling client requests from there. The server instructs the client when to change to a particular scene and once loaded spawns the required network objects on the client.
     
    makeshiftwings likes this.
  3. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    How would you handle something like NPC movement on the server? I'd like to use NetworkTransform and have it walk around on a mesh collider on the server but it seems like if there were creatures in two different scenes reacting to players, the server would have to load both additively into one scene and either put the scenes right on top of each other and use Layers to determine which meshes to use for collision or to separate them spatially and hope that nothing overlaps.
     
  4. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    664
    I can't offer much guidance on the specifics as my Unity knowledge is pretty limited outside I've what I've needed so far. I'm creating a simple strategy game where players enter a lobby, create a room, then from there a new self-contained instance of a game is started.

    The main approach for each scene is that when a client is in that scene only the objects of that particular scene are spawned on that client. For example when a player joins the game they're placed in the lobby scene and objects for each player in the lobby and objects for available rooms are spawned on that client. If they choose to join a room all player and room objects are despawned, they switch to the room scene, then the room and room player objects are spawned in. The game instance scene follows the same approach, just with a lot more going on.

    The key point is that clients only have objects spawned that are relevant to them in that scene and aren't aware of other clients in other scenes, it being the server's job to keep track of it all.
     
    makeshiftwings likes this.
  5. Zendoth

    Zendoth

    Joined:
    Dec 27, 2020
    Posts:
    1
    The solution I came up with was to hide the sprite of the other player if not in the same scene.

    Just curious with your solution. It is possible to have the network manager in a single scene and scenes without the scene manager can still use it? Curious how that works and your implementation.

    Currently for me the network manager is don'tdestroyonload so when a player goes to a new scene the network manager follows the player from scene to scene.
     
  6. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    664
    I'm not sure I follow but to clarify the network manager will be in all scenes due to as you say its set as DontDestroyOnLoad. I don't have a scene manager as such, just a manager handling events which trigger which scene to put the client in and objects it should receive. This is running as a dedicated server so there can't be a host.

    One important point to make is when scene management is disabled the server doesn't actually care what scene the client is in and the client can switch scenes as it likes with the server being none the wiser. What the client can't do is switch scenes once it contains network objects as the server will have to despawn them first before the client can switch to another scene.