Search Unity

Third Party Photon - Cameras not working when more than one exist

Discussion in 'Multiplayer' started by DnT_Games, Jun 9, 2019.

  1. DnT_Games

    DnT_Games

    Joined:
    May 31, 2016
    Posts:
    3
    I have a room for multiplayer where the players join and then click the "Spawn" button to instantiate their player and the camera. The instantiation works, both players spawn with their respective cameras. The host player and camera work when it is just the host player and camera, and the client side works as well. The problem is that when a client joins the room and clicks the spawn button, the host's camera stops moving and won't follow the host. It doesn't follow the client either it just sort of sits there. The client side works flawlessly. Below is the GameManager script that instantiates the players/cameras and the Camera script.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GameManager : MonoBehaviour {
    6.  
    7.     public GameObject Player_PREFAB;
    8.     public GameObject hostCamera;            // Camera for Host
    9.     public GameObject clientCamera;          // Camera for the user joining
    10.  
    11.     public GameObject MainCanvas;
    12.     public GameObject PlayerCanvas;
    13.  
    14.     public GameObject SceneCam; // Camera for when player has not clicked 'spawn' button
    15.  
    16.     public GameObject Spawn1; //Location for spawnpoint 1
    17.     public GameObject Spawn2; //location for spawnpoint 2
    18.  
    19.     public static int NumPlayers = PhotonNetwork.room.PlayerCount;
    20.     public static bool tankInScene;
    21.  
    22.     private void Awake()
    23.     {
    24.         MainCanvas.SetActive(true);
    25.         PlayerCanvas.SetActive(false);
    26.  
    27.         if (NumPlayers > 1)
    28.         {
    29.             tankInScene = true;
    30.         }
    31.         else
    32.         {
    33.             tankInScene = false;
    34.         }
    35.     }
    36.  
    37.     public void SpawnPlayer(){
    38.  
    39.      
    40.         Debug.Log(NumPlayers);
    41.  
    42.         float spawnRange = Random.Range (-50f, 50f);
    43.         Debug.Log("SPAWNING PLAYER...");
    44.  
    45.             if (tankInScene == false) // If we are the host
    46.             {
    47.                 PhotonNetwork.Instantiate(hostCamera.name, new Vector3(((hostCamera.transform.position.x) + (spawnRange)) + (180 + spawnRange), hostCamera.transform.position.y, hostCamera.transform.position.z), hostCamera.transform.rotation, 0);
    48.                 PhotonNetwork.Instantiate(Player_PREFAB.name, new Vector3((Spawn1.transform.position.x) + (spawnRange), Spawn1.transform.position.y, Spawn1.transform.position.z), Player_PREFAB.transform.rotation, 0);
    49.                 Player_PREFAB.name = "Player1";
    50.             }
    51.             else // If we are a client
    52.             {
    53.                 PhotonNetwork.Instantiate(clientCamera.name, new Vector3(((clientCamera.transform.position.x) + (spawnRange)) + (180 + spawnRange), clientCamera.transform.position.y, clientCamera.transform.position.z), clientCamera.transform.rotation, 0);
    54.                 PhotonNetwork.Instantiate(Player_PREFAB.name, new Vector3((Spawn1.transform.position.x) + (spawnRange), Spawn1.transform.position.y, Spawn1.transform.position.z), Player_PREFAB.transform.rotation, 0);
    55.                 Player_PREFAB.name = "Player2";
    56.             }
    57.      
    58.  
    59.      
    60.  
    61.         MainCanvas.SetActive (false);
    62.      
    63.         PlayerCanvas.SetActive (true);
    64.         SceneCam.SetActive (false);
    65.  
    66.  
    67.     }
    68.  
    69.     private void Update()
    70.     {
    71.      
    72.         Debug.Log(tankInScene + ": TANK IN SCENE");
    73.  
    74.         if (NumPlayers > 1)
    75.         {
    76.             tankInScene = true;
    77.         }
    78.         else
    79.         {
    80.             tankInScene = false;
    81.         }
    82.  
    83.         Debug.Log(NumPlayers + ": THE NUMBER OF PLAYERS IN THE ROOM - From GameManager Update Function.");
    84.     }
    85.  
    86. }
    87.  



    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class CamerControllerMultiplayer : Photon.MonoBehaviour {
    6.  
    7.     private bool doMovement = true;
    8.  
    9.     public GameObject player;
    10.     private Vector3 offset;
    11.     Vector3 position;
    12.  
    13.     bool exists = false;
    14.  
    15.     static int cameraNumber;
    16.  
    17.  
    18.     private void Start()
    19.     {
    20.  
    21.         if (photonView.isMine)
    22.         {
    23.             Debug.Log("Is Mine. Looking for player...");
    24.             cameraNumber = GameManager.NumPlayers;
    25.             StartCoroutine(GetInfo());
    26.             Debug.Log("Loading Get Info...");
    27.             Debug.Log(offset);
    28.         }
    29.     }
    30.  
    31.     void FixedUpdate(){
    32.  
    33.         if (photonView.isMine) {
    34.             this.gameObject.transform.position = player.transform.position + offset;
    35.  
    36.  
    37.             Debug.Log("Tracking " + player);
    38.  
    39.             Debug.Log(offset);  //Offset does not change when client comes in but camera does not move with host
    40.         }
    41.  
    42.     }
    43.  
    44.     IEnumerator GetInfo()
    45.     {
    46.  
    47.         yield return new WaitForSecondsRealtime(1);
    48.  
    49.         if (photonView.isMine)
    50.         {
    51.             if (GameManager.tankInScene == false)
    52.             {
    53.                 player = GameObject.Find("Destination1");
    54.                 offset = new Vector3(player.transform.position.x, player.transform.position.y + 300, player.transform.position.z);
    55.                 Debug.Log("Found " + player);
    56.             }
    57.             else if (GameManager.tankInScene == true)
    58.             {
    59.                 player = GameObject.Find("Destination2");
    60.                 offset = new Vector3(player.transform.position.x, player.transform.position.y + 300, player.transform.position.z);
    61.                 Debug.Log("Found " + player);
    62.  
    63.  
    64.             }
    65.         }
    66.     }
    67.  
    68.  
    69.  
    70.  
    71.  
    72.  
    73. }
    74.  
     
    Last edited: Jun 9, 2019
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    What is the purpose of the camera being a networked GameObject? Are you allowing players to view the camera of other players?

    Otherwise, you should just instantiate the camera locally and have it follow the client's own character.
     
  3. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    I'm unsure why you are giving the 2nd client it's own camera, is this a split screen game like MarioKart?
     
  4. DnT_Games

    DnT_Games

    Joined:
    May 31, 2016
    Posts:
    3
    I changed both cameras to be instantiated locally and it solved the problem
     
    Joe-Censored likes this.
  5. 13E12K

    13E12K

    Joined:
    May 4, 2014
    Posts:
    20
    I have a similar issue like the OP and dont want to go with instantiating camera solution.

    I have used official PUN tutorial and extended it with my own prefabs with success. I can join rooms with multiple clients and instantiate my ship prefab.

    Basic concept is that there is a gameobject called "CameraPivot" with "MainCamera" as its child object. Whenever, a player joins, the CameraPivot object has to be attached to the instantiated player ship locally. If there is only the host, it works OK. When a client joins, the camera works for the client side as well. However, the CameraPivot object with MainCamera gets reset to intial position for the host and camera gets locked. I connected up to 5 clients to the host, and all the clients work flawless but the host.


    Also, when I created and joined the scene with 6 players, the problem occured to be related only to the host player. I disconnected the host, the second player becomes the host. And right after the second player becomes host, it loses the camarapivot with maincamera. When I disconnected the second player which is the host now, the same happens with the 3rd player.

    I tried assigning the camerapivot both from gamemanager and the player prefab. Below is the GameManager script that instantiates the players/cameras and the Camera script. So anyone has any idea why this happens or how to overcome this issue? (I am quite new to both Unity and Photon, so I might have overlooked something easy)


    Code (CSharp):
    1. void Start()
    2. {
    3. Instance = this;
    4. if (playerPrefab == null)
    5. {
    6. Debug.LogError("<Color=Red><a>Missing</a></Color> playerPrefab Reference. Please set it up in GameObject 'Game Manager'", this);
    7. }
    8. else if (ShipManager.LocalPlayerInstance == null)
    9. {
    10. Debug.LogFormat("We are Instantiating LocalPlayer from {0}", SceneManagerHelper.ActiveSceneName);
    11. // we're in a room. spawn a character for the local player. it gets synced by using PhotonNetwork.Instantiate
    12. GameObject LocalShip = PhotonNetwork.Instantiate(this.playerPrefab.name, new Vector3(UnityEngine.Random.Range(0, 200), 3, 0f), Quaternion.identity, 0);
    13. if (!LocalShip.GetPhotonView().IsMine)
    14. {
    15. Camera.main.GetComponent<CameraOrbit>().enabled = false;
    16. }
    17. else
    18. {
    19. Camera.main.GetComponent<CameraOrbit>().enabled = true;
    20.  
    21. CamPivot = GameObject.Find("CameraPivot");
    22.  
    23. CamPivot.GetComponent<CameraSystem>().shipView = LocalShip.GetComponent<CameraAssign>().sView;
    24. CamPivot.GetComponent<CameraSystem>().portsideView = LocalShip.GetComponent<CameraAssign>().prtView;
    25. CamPivot.GetComponent<CameraSystem>().starboardView = LocalShip.GetComponent<CameraAssign>().stbView;
    26.  
    27. Camera.main.GetComponent<CameraOrbit>().victory = LocalShip;
    28. }
    29. }
    30. else
    31. {
    32. Debug.LogFormat("Ignoring scene load for {0}", SceneManagerHelper.ActiveSceneName);
    33. }
    34. }
     
  6. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
  7. 13E12K

    13E12K

    Joined:
    May 4, 2014
    Posts:
    20
    tobiass likes this.