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;
}
}
UPDATE - AFTER YOUR COMMENT
You're sending the commands to all players.
Before sending the command you should check if the player receiving the command is OWNER of the new player.
Basically you're doing this.
=> CREATE PLAYER
=> ASSIGN CAMERA
When you should to this.
=> CREATE PLAYER
=> CHECK IF THIS YOUR CLIENT => ASSIGN CAMERA
To achieve that you need to add this check:
if (photonView.IsMine) AssignCamera();
I see that you're sending the command in the manager. So you have these solutions:
public class YourPlayer : Photon.MonoBehaviour
{
public void Start()
{
//stop assigning controls if this is not the player related to this peer
if(!photonView.IsMine) return;
player.GetComponent<FireFighterHeroController>().enabled = true;
player.GetComponent<CameraControler>().enabled = true;
player.GetComponent<CameraControler>().SetTarget(player.transform);
}
}
I'm not sure about all the logic of your code. Some code should apply to all players, some other code should apply only to Peer Related player.
I think you should just change the Start method like so:
public void Start()
{
GameObject player = PhotonNetwork.Instantiate(this.playerPrefab.name, spawnPoint.position, Quaternion.identity);
//stop assigning controls if this is not the player related to this peer
if(!player.GetPhotonView().IsMine) return;
player.GetComponent<FireFighterHeroController>().enabled = true;
player.GetComponent<CameraControler>().enabled = true;
player.GetComponent<CameraControler>().SetTarget(player.transform);
}
Further info: Player Networking - Photon PUN
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With