Search Unity

Question Transition Scenes with Mirror Networking

Discussion in 'Multiplayer' started by aouniaouni, Mar 16, 2024.

  1. aouniaouni

    aouniaouni

    Joined:
    Jul 6, 2019
    Posts:
    2
    Hello there
    I'm making a simple coop multiplayer game.

    I want to achieve the following:
    There is a scene called "scene1" which is an outdoor environment and a house.
    "TransitionScene" is the entrance room of that house. One door leads to scene1 and another one to scene2.
    "scene2" is the rest of the house (indoor).
    The players start in the environment level, but the transition scene should already be loaded in. Once all players have entered the house (and thus the transition scene), the door behind them closes and the outdoor scene should be unloaded. After all clients have loaded the indoors scene (scene2), the second door leading to the rest of the house opens. After they entered that scene, the door can close and lock again and then, the transition scene can be unloaded.

    Essentially I don't want the players to notice the loading.

    I know that Mirror supports additive scenes and subscenes, but if I understood correctly, only players in the same subscene get synchronized, so if in my example a player were to look into the first room (TransitionScene), it wouldn't show the other player moving, even though he is in there.

    Is there any way to achieve this kind of transition with mirror or will I have to use loading screens?

    Thanks in advance!
     
  2. andrinz

    andrinz

    Joined:
    Aug 15, 2019
    Posts:
    1
    Fantastic Question!! I have the exact same problem, and its resolve is of great Importance.
     
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,012
    Independently of the networking solution, this can be achieved as follows:
    • when the player tries to open the door, additively (async) load the transition scene
    • when the transition scene is loaded, the door opens
    • the reverse happens when unloading a scene
    Since operating the door loads the scene and that may take a few seconds, you may want to have a door-open animation that's long enough for the scene to load and which can loop for a while without looking odd (ie player pulling the door, door not budging for a while until it opens).

    It may also be helpful to set up the transition scene (or any additive scene) so that the visuals are in a common root object, so that you can hide/show all of the scene's content at once and at your discretion.

    All scene content should be loaded additively, including your main game scene (the one that gets single-loaded). This scene should be visually empty besides whatever scripts and content needs to be always visible and active (eg Managers, GUI). All other content is loaded additively and set active or inactive as needed.

    Player synchronization should occur within the same single-loaded scene. Additive scenes just add more content but should not affect player synchronization. But I don't know how Mirror works in that regard.

    Btw, Subscenes are a concept not normally found in Unity but only in Unity's Entity Component System. Mirror uses that term incorrectly, where they say "SubScene" they really mean to say "additive scene".
     
    Last edited: Mar 16, 2024
  4. aouniaouni

    aouniaouni

    Joined:
    Jul 6, 2019
    Posts:
    2
    Thanks for the answer!

    I know how to load an additive scene, that's not the problem, it really is Mirror specific (I guess I should have specified that, sorry for that).

    In the end, I managed to figure out a workaround.
    Here it is (in case anyone else has this problem):
    - Make a game object that is the transition "scene" (and contains all of the elements in the transition room and the room itself)
    - Add a trigger to that object
    - Add a script that closes the entrance door (of the transition scene) once all players have entered the transition scene and then adds itself and the whole "scene" to DontDestroyOnLoad
    - Change the networked scene to the second one (using mirrors ServerChangeScene, which uses LoadSceneAsync)
    - Once all clients have loaded the scene, open the exit door of the transition scene and add the transition scene to the newly loaded scene (= remove it from DontDestroyOnLoad) so that it will be unloaded for the next scene transition