Search Unity

  1. We have added dedicated sub-forums for UNet, the experimental MLAPI and Unity Transport Package. Please use the new forums to discuss related topics.
    Dismiss Notice
  2. How can our website serve you better? Give us your feedback. Take our survey and let us know.
    Dismiss Notice

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,559
    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,559
    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,364
    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!
     
    Joe-Censored likes 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
     
  8. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,364
    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,364
    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. esuzen

    esuzen

    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,364
    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?
     
unityunity