0
\$\begingroup\$

I have already successfully created a multiplayer scene where multiple users can join, however there are some issues like unity not allowing multiple sound listeners per scene. In order to alleviate this problem (hopefully) I would like to load an additive scene that connects to the master client. In the additive scenes will contain the other players. How can I achieve this using photon PUN2?

In normal unity non multiplayer it is a easy as SceneManager.LoadScene(sceneName,LoadSceneMode.Additive); With that code a new scene adds on to the scene already at play as a new scene. I wish to do so with photon PUN2 somehow, where the master client loads his scene and other players join the game using their own scenes. Currently the other players are loading into the scene with a simple instantiation of their prefabs PhotonNetwork.Instantiateand this is giving me errors such as the one stated above (multiple sound listeners)

Please help

\$\endgroup\$
4
  • \$\begingroup\$ Spawning the content needed for a second player is usually done by instantiating prefabs, not additive scene loading. Things like the camera with its attached audio listener are usually spawned locally — not replicated between clients (usually we want each client to render just their own view, not everyone's view) \$\endgroup\$
    – DMGregory
    Commented Apr 6, 2020 at 10:46
  • \$\begingroup\$ @DMGregory what do you mean "spawned locally"? Cuz my camera and audio listener are sitting in the prefab player object that spawns, should it not? \$\endgroup\$ Commented Apr 6, 2020 at 20:39
  • \$\begingroup\$ Does client A need their scene to contain client B's camera? Generally not. So the prefab you spawn over the network should not contain the camera. Client B can then spawn a separate camera prefab on their machine alone ("locally") and parent it to their copy of the player object, without that camera being replicated to all other clients. \$\endgroup\$
    – DMGregory
    Commented Apr 6, 2020 at 20:41
  • \$\begingroup\$ Oh that sounds very smart, thanks, Ill test it out and return with my results thanks! \$\endgroup\$ Commented Apr 6, 2020 at 20:47

2 Answers 2

2
\$\begingroup\$

This might not be the most elegant solution but it's certainly an effective one:

What I have done in the past when using PUN for this is to use the PhotonNetwork.Instantiate() method for player instantiation. What you to avoid issues with multiple audio listeners and cameras is to write a NetworkPlayerManager script and add it to the player prefab. In this script in the Start() method you add a simple check to see if the player is the local player or a remote player and if it's a remote player you can disable the camera and audio listener (as well as any UI/Input management, etc...) components.

void Start() {
  if (photonView.IsMine) {
    //Any initialisation required for the local player goes here.
  } else {
    foreach(MonoBehaviour c in disableComponentsIfNotMine)
      c.enabled = false;
    foreach(GameObject g in disableGameObjectsIfNotMine)
      g.SetActive(false);
  }

  DontDestroyOnLoad(gameObject);
}
\$\endgroup\$
1
  • \$\begingroup\$ This sounds pretty good, I'll try it, but where does "disableGameObjectsIfNotMine" and "disableComponentsIfNotMine" come in and how are they implemented? \$\endgroup\$ Commented Apr 6, 2020 at 20:37
1
\$\begingroup\$

I basically combined Thomas Mathieson's answer combined with DMGregory's response to successfully add two players. I used if (photonView.IsMine) and within it I placed DMGregory's suggestion to instantiate the player objects such as camera and audio listener locally.An empty player object is spawned in through Photon and within that empty player object is the script that instantiates the player cam and audio listener locally. Thanks! –

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .