I making 2D game, the problem is that when the first player creates and appears in the room his camera is working normally, but when a new player enters the room, the first player sees everything through the camera of the second player. Further, if a third player connects, the first and second players see everything through the third player’s camera and so on.
Player prefab instantiated in the GameManager class which is only on the game scene
public class GameManager : MonoBehaviourPunCallbacks
{
public GameObject playerPrefab;
public Transform spawnPoint;
public void Start()
{
GameObject player = PhotonNetwork.Instantiate(this.playerPrefab.name, spawnPoint.position, Quaternion.identity);
player.GetComponent<FireFighterHeroController>().enabled = true;
player.GetComponent<CameraControler>().enabled = true;
player.GetComponent<CameraControler>().SetTarget(player.transform);
}
#region PUN Callbacks
public override void OnLeftRoom()
{
SceneManager.LoadScene(1);
}
#endregion
#region Custom Methods
public void OnClickLeaveRoom_Btn()
{
PhotonNetwork.LeaveRoom();
}
#endregion
}
Simple camera controller
public class CameraControler : MonoBehaviour
{
private Transform target;
public GameObject camera;
public Vector3 offset;
public void SetTarget(Transform target)
{
this.target = target;
}
public void LateUpdate()
{
camera.transform.position = target.position + offset;
}
}
UPD Found a solution
I moved the script of the camera controller to the player’s camera, and in the player’s instance I search for this camera, turn it on and transfer the player’s transform to it.
Not sure about this method, but it at least works.
Here is that monster
public class GameManager : MonoBehaviourPunCallbacks
{
public Transform spawnPoint;
public void Start()
{
GameObject player = PhotonNetwork.Instantiate("Player", spawnPoint.position, Quaternion.identity);
if (!player.GetPhotonView().IsMine)
return;
player.GetComponent<FireFighterHeroController>().enabled = true;
player.transform.Find("Camera").gameObject.GetComponent<CameraControler>().enabled = true;
player.transform.Find("Camera").gameObject.GetComponent<CameraControler>().SetTarget(player.transform);
player.transform.Find("Camera").gameObject.SetActive(true);
}
}
And Camera Controller
public class CameraControler : MonoBehaviour
{
private Transform target;
public Vector3 offset;
public void SetTarget(Transform target)
{
this.target = target;
}
public void LateUpdate()
{
gameObject.transform.position = target.position + offset;
}
}