Search Unity

Third Party Camera issues with Mirror multiplayer

Discussion in 'Multiplayer' started by Thirdlioncell, May 18, 2020.

  1. Thirdlioncell

    Thirdlioncell

    Joined:
    Apr 28, 2020
    Posts:
    5
    Hello,

    I am using the Mirror extension and experimenting with a multiplayer FPS style game. The NetworkManager auto spawns a player prefab when someone joins the game. The player prefab his children transforms most notably a camera.

    When client 1 starts a new server and spawns in using the player prefab everything works great. The first person camera works and the controls are great!

    However, when client 2 joins the game client 1 is hosting, client 1's view becomes client 2's view. Client 1 is no longer viewing the camera attached to his player prefab. Instead he is viewing the camera attached to client 2's prefab. I am guessing this has to do with client authority and I've done many google searches but I cannot figure out where I have gone wrong! Help would be appreciated.
     
  2. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    It means you aren't disabling the camera on the player 2 gameobject in player 1's client (and vice versa I assume).

    I can't help with specific coding because I don't use Mirror, however, if you post some of the network code for your player objects someone should be able to help if you are still stuck.
     
  3. Thirdlioncell

    Thirdlioncell

    Joined:
    Apr 28, 2020
    Posts:
    5
    Thanks for the reply! I came to a conclusion around there but then figured out that the cameras have the same name. So I don't know how to enable or disable if they have the same name... Is there any other object identifiers I could use?
     
  4. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    There should be a way to determine if the script is running on the owning client or the remote client, in Photon PUN this is achieved by querying photonView.IsMine and disabling the relevant components when it has a value of false.

    There is probably an equivalent property in Mirror, but I don't know what it is.

    Hopefully, someone familiar with Mirror will chip in and help.
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    It is a bad idea to make your camera part of a networked object's prefab. Just attach your camera to the object which represents that client after it is spawned in, and use an empty GameObject on the prefab as a mount point for the camera. Check for "isLocalPlayer" to determine which one to attach to.

    Something like this:
    Code (csharp):
    1. public GameObject CameraMountPoint;
    2.  
    3. void Start()
    4. {
    5.     if (isLocalPlayer)
    6.     {
    7.         Transform cameraTransform = Camera.main.gameObject.transform;  //Find main camera which is part of the scene instead of the prefab
    8.         cameraTransform.parent = CameraMountPoint.transform;  //Make the camera a child of the mount point
    9.         cameraTransform.position = CameraMountPoint.transform.position;  //Set position/rotation same as the mount point
    10.         cameraTransform.rotation = CameraMountPoint.transform.rotation;
    11.     }
    12. }
     
    Last edited: May 18, 2020
  6. Thirdlioncell

    Thirdlioncell

    Joined:
    Apr 28, 2020
    Posts:
    5
    Ahh GENIUS! Thanks a lot I'll give it a shot and im sure it will work!
     
    adenherr21, sunkit02 and Joe-Censored like this.
  7. Thirdlioncell

    Thirdlioncell

    Joined:
    Apr 28, 2020
    Posts:
    5
    Hmm your code seemed right and everything went through with no errors until I launched the game. I got an error talking about a Null Reference Exception and no game object being found. I am not sure whats causing this and I went back through code several times and could not sort it out. Any ideas? Probably being super stupid... I'm a Unity newbie so sorry for taking up your time
     
    sunkit02 likes this.
  8. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    So there's only two references which can be null there. There is Camera.main, which will find the active camera in the scene with the tag "MainCamera". If there is no active Camera in the scene with that tag, it will return null.
    https://docs.unity3d.com/ScriptReference/Camera-main.html

    The second is CameraMountPoint. You fill in that field in the inspector on the prefab itself. I would just use your old Camera GameObject that is already a part of the prefab, remove its Camera component, and assign it to that field. If it is not filled in in the Inspector then when you go to use its transform it will generate a null reference exception.
     
  9. Thirdlioncell

    Thirdlioncell

    Joined:
    Apr 28, 2020
    Posts:
    5
    Alright yep it was just me not tagging the camera. My bad. Thanks for your help really appreciate the Unity community
     
    Joe-Censored likes this.
  10. DERPGamezOfficial

    DERPGamezOfficial

    Joined:
    Jan 12, 2020
    Posts:
    1
    OMG I had this same issue and this solution worked. Thank you so much!!!
     
    Joe-Censored likes this.
  11. AllofPaul

    AllofPaul

    Joined:
    May 3, 2020
    Posts:
    2
    for me, for some reason setting parent only worked when i did

    Code (CSharp):
    1. cameraTransform.SetParent(cameraMountPoint.transform);
     
  12. mathisfchx

    mathisfchx

    Joined:
    Feb 26, 2021
    Posts:
    1
    Hi, i'm sorry for the inconvenience but i'm facing the exact problem.
    I've added the code about isLocalPlayer to the player prefab, but if a attach a prefab of an empty gameobject in cameramount i've got an error that says that i can't set the parent of a prefab.
    As our player which represents the client is a prefab spawned buy the networkManager, I can't just set an empty gameObject to it.
     
  13. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If you're referring to my post from a year ago, I was referring to making the empty GameObject a part of the prefab itself. It also isn't the parent, but a child. Basically it is just used as a mount point for the camera, while not actually including the camera in the prefab.
     
  14. ZyadRedaa

    ZyadRedaa

    Joined:
    Mar 2, 2021
    Posts:
    1
    Thanks dude, that worked just fine!
     
    Joe-Censored likes this.
  15. VictoryPlayerUmer

    VictoryPlayerUmer

    Joined:
    Aug 27, 2020
    Posts:
    2
    It works. It goddamn works. You are a life saver man
     
  16. GoldenPandaTV

    GoldenPandaTV

    Joined:
    Apr 28, 2017
    Posts:
    13

    Hi, I follow your method but I have an issue, when a second player appear on the server, the camera is going in his mountpoint, did I miss a setup ?
     
  17. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Are you checking isLocalPlayer first?
     
  18. Jonnypopcorn

    Jonnypopcorn

    Joined:
    Jun 20, 2021
    Posts:
    1
    I did everything u said but my camera won’t mount on the empty game object. Do you know what could’ve gone wrong?
     
    italoazevedo92 likes this.
  19. italoazevedo92

    italoazevedo92

    Joined:
    Oct 18, 2020
    Posts:
    1
    You guys have the answer in the mirror examples :)

    just add the script to the player prefab


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Mirror;
    5. using UnityEngine.SceneManagement;
    6.  
    7. // This sets up the scene camera for the local player
    8.  
    9. namespace Mirror.Examples.NetworkRoom
    10. {
    11.     public class PlayerCamera : NetworkBehaviour
    12.     {
    13.         Camera mainCam;
    14.  
    15.         void Awake()
    16.         {
    17.             mainCam = Camera.main;
    18.         }
    19.  
    20.         public override void OnStartLocalPlayer()
    21.         {
    22.             if (mainCam != null)
    23.             {
    24.                 // configure and make camera a child of player with 3rd person offset
    25.                 mainCam.orthographic = false;
    26.                 mainCam.transform.SetParent(transform);
    27.                 mainCam.transform.localPosition = new Vector3(0f, 8f, -15f);
    28.                 mainCam.transform.localEulerAngles = new Vector3(15f, 0f, 0f);
    29.             }
    30.         }
    31.  
    32.         public override void OnStopClient()
    33.         {
    34.             if (isLocalPlayer && mainCam != null)
    35.             {
    36.                 mainCam.transform.SetParent(null);
    37.                 SceneManager.MoveGameObjectToScene(mainCam.gameObject, SceneManager.GetActiveScene());
    38.                 mainCam.orthographic = true;
    39.                 mainCam.transform.localPosition = new Vector3(0f, 70f, 0f);
    40.                 mainCam.transform.localEulerAngles = new Vector3(90f, 0f, 0f);
    41.             }
    42.         }
    43.     }
    44. }
    45.  
     
    MurphyMurph_21 and jijameschoi like this.
  20. AHumanIBelieve

    AHumanIBelieve

    Joined:
    Aug 18, 2020
    Posts:
    1
    Even easier -> Have the camera in the player prefab, and enable it with
    onstartlocalplayer
    .
    Code (CSharp):
    1. public GameObject cam;
    2.  
    3. public override void OnStartLocalPlayer(){
    4.         cam.SetActive(true);
    5.         Debug.Log("camera on");
    6.     }
     
    GhostlyMidnight, SatoruG and Ffix like this.
  21. rgd1998

    rgd1998

    Joined:
    Jul 25, 2022
    Posts:
    3
    (Please forgive my poor English) Hello, I have encountered a scenario where I have to put a camera in the prefab: because I am making a vr game, I have to use the [CameraRig] prefab of SteamVR Plugin, which already has a camera in the prefab, and this camera is the screen displayed in the VR eye. What should I do in this situation?
     
  22. SatoruG

    SatoruG

    Joined:
    Feb 13, 2021
    Posts:
    1
    xd its works, thx so much