Search Unity

Third Party Setting up multiple cameras in Mirror

Discussion in 'Multiplayer' started by Uderfrytke, Mar 30, 2022.

  1. Uderfrytke

    Uderfrytke

    Joined:
    Jun 5, 2020
    Posts:
    4
    Hey guys,

    I am trying to make a multiplayer game with Mirror, but I am still new to networking. I am currently trying to set up a scene with 3 cameras (see attached image). Every player should see every camera. To display the cameras of Player1 and Player2 separately, I am adjusting the Viewport Rect. The player-cameras are child objects of the player prefabs.
    My problem is now, that if a player joins a multiplayer server, the Viewport Rect of his camera has the values of the prefab and I don't know how to set the correct Viewport Rect and make it visible for all players.
    I tried setting it up in the NetworkManager like this:

    Code (CSharp):
    1.  
    2. public class MyNetworkManager : NetworkManager
    3. .
    4. .
    5. .    
    6.     public override void OnServerAddPlayer(NetworkConnectionToClient conn)
    7.     {
    8.         base.OnServerAddPlayer(conn);
    9.         SetClientCamera(_clients, conn);
    10.     }
    11.  
    12.     private void SetClientCamera(int client, NetworkConnectionToClient conn)
    13.     {
    14.         Camera cam = conn.identity.GetComponentInChildren<Camera>();
    15.         if (client == 0)
    16.         {
    17.             cam.rect = new Rect(0.068f, 0.025f, 0.24f, 0.33f);
    18.         }
    19.         else
    20.         {
    21.             cam.rect = new Rect(0.655f, 0.025f, 0.24f, 0.33f);
    22.         }
    23.         _clients++;
    24.     }
    25.  
    This works only partly because for the server the cameras are displayed correctly, but not for the players. I think for the players I need to set it up with a NetworkBehaviour Script but I don't know how exactly.
    I tried similar code as above in the player script which inherits from NetworkBehaviour with the [Client], [Command] and [ClientRpc] flags but nothing worked.

    Is there a good practice how to deal with multiple cameras in Mirror?
    Help would be very much appreciated.
     

    Attached Files:

  2. Uderfrytke

    Uderfrytke

    Joined:
    Jun 5, 2020
    Posts:
    4
    I solved the problem by creating a script inheriting from NetworkBehaviour and attaching it to the player object. It is nearly the same code as I have posted above:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Mirror;
    5.  
    6. public class NetworkCamera : NetworkBehaviour
    7. {
    8.     Camera baseplateCam;
    9.  
    10.     public override void OnStartClient()
    11.     {
    12.         base.OnStartClient();
    13.         baseplateCam = GetComponentInChildren<Camera>();
    14.         if (isLocalPlayer)
    15.         {
    16.             baseplateCam.rect = new Rect(0.068f, 0.025f, 0.24f, 0.33f);
    17.         }
    18.         else
    19.         {
    20.             baseplateCam.rect = new Rect(0.655f, 0.025f, 0.24f, 0.33f);
    21.         }
    22.     }
    23. }
    24.  
    Now the only problem is, that the local player will always see his baseplate on the left and the other player's on the right, but I think I can live with that.