Search Unity

Photon - Cameras not working when more than one exist

Discussion in 'Connected Games' started by Flipside_LLC, Jun 9, 2019.

  1. Flipside_LLC

    Flipside_LLC

    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:
    6,305
    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,237
    I'm unsure why you are giving the 2nd client it's own camera, is this a split screen game like MarioKart?
     
  4. Flipside_LLC

    Flipside_LLC

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