Hi,
I’ve been following the “Get Started” tutorial on the official Photon PUN website, and tried to implement multiplayer on the go on a small project that I had.
Tutorial : Pun 2 0 - Introduction | Photon Engine
I followed everything smoothly until the part where the players position need to be synced accross all
session with a Photon Transform View component. My problem is that the clients are not updating the other player position.
Here is a video of what I mean :
Here is a screen capture of my player prefab :
Here is my Launch script (it creates a room for the player and change the scene when there are two players in the same room) :
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
public class Launch : MonoBehaviourPunCallbacks
{
[Tooltip("The maximum number of players per room. When a room is full, it can't be joined by new players, and so new room will be created")]
[SerializeField]
private byte maxPlayersPerRoom = 4;
[Tooltip("The Ui Panel to let the user enter name, connect and play")]
[SerializeField]
private GameObject controlPanel;
[Tooltip("The UI Label to inform the user that the connection is in progress")]
[SerializeField]
private GameObject progressLabel;
private readonly string gameVersion = "1";
private bool isConnecting;
private void Awake()
{
PhotonNetwork.AutomaticallySyncScene = true;
}
private void Start()
{
controlPanel.SetActive(true);
progressLabel.SetActive(false);
}
public void Connect()
{
controlPanel.SetActive(false);
progressLabel.SetActive(true);
if (PhotonNetwork.IsConnected)
{
PhotonNetwork.JoinRandomRoom();
}
else
{
isConnecting = PhotonNetwork.ConnectUsingSettings();
PhotonNetwork.ConnectUsingSettings();
PhotonNetwork.GameVersion = gameVersion;
}
}
public override void OnConnectedToMaster()
{
Debug.Log("Launcher: OnConnectedToMaster() was called by PUN");
if (isConnecting)
{
PhotonNetwork.JoinRandomRoom();
isConnecting = false;
}
}
public override void OnDisconnected(DisconnectCause cause)
{
controlPanel.SetActive(true);
progressLabel.SetActive(false);
isConnecting = false;
Debug.LogWarningFormat("Launcher: OnDisconnected() was called by PUN with reason {0}", cause);
}
public override void OnJoinRandomFailed(short returnCode, string message)
{
Debug.Log("Launcher:OnJoinRandomFailed() was called by PUN. No random room available, so we create one.\nCalling: PhotonNetwork.CreateRoom");
PhotonNetwork.CreateRoom(null, new RoomOptions {MaxPlayers = maxPlayersPerRoom });
}
public override void OnJoinedRoom()
{
Debug.Log("Launcher: OnJoinedRoom() called by PUN. Now this client is in a room.");
}
private void LoadArena()
{
if (!PhotonNetwork.IsMasterClient)
{
Debug.LogError("PhotonNetwork : Trying to Load a level but we are not the master Client");
}
Debug.LogFormat("PhotonNetwork : Loading Arena");
PhotonNetwork.LoadLevel("SampleScene");
}
public override void OnPlayerEnteredRoom(Player newPlayer)
{
Debug.LogFormat("OnPlayerEnteredRoom() {0}", newPlayer.NickName);
if (PhotonNetwork.IsMasterClient && PhotonNetwork.CurrentRoom.PlayerCount == 2)
{
Debug.LogFormat("OnPlayerEnteredRoom IsMasterClient {0}", PhotonNetwork.IsMasterClient);
LoadArena();
}
}
}
I made some modification to the Launch script from the tutorial, I wanted to wait for both player to join the room before loading the scene. It’s working fine.
Here is my Game Manager script (it handles game behaviour when a player leaves the room) :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using Photon.Pun;
using Photon.Realtime;
public class GameManager : MonoBehaviourPunCallbacks
{
private Vector3 position;
[Tooltip("The prefab to use for representing the player")]
public GameObject playerPrefab;
private void Start()
{
if (PhotonNetwork.IsMasterClient)
{
position = new Vector3(-4f, 0.5f, 9f);
}
else
{
position = new Vector3(4f, 0.5f, 9f);
}
if (playerPrefab == null)
{
Debug.LogError("<Color=Red><a>Missing</a></Color> playerPrefab Reference. Please set it up in GameObject 'Game Manager'", this);
}
else
{
PhotonNetwork.Instantiate(this.playerPrefab.name, position, Quaternion.identity, 0);
}
}
public override void OnLeftRoom()
{
SceneManager.LoadScene(0);
}
public void LeaveRoom()
{
PhotonNetwork.LeaveRoom();
}
public override void OnPlayerLeftRoom(Player otherPlayer)
{
Debug.LogFormat("OnPlayerLeftRoom() {0}", otherPlayer.NickName);
if (PhotonNetwork.IsMasterClient)
{
Debug.LogFormat("OnPlayerLeftRoom IsMasterClient {0}", PhotonNetwork.IsMasterClient);
LeaveRoom();
}
}
}
I’m asking because after many google research I only found posts where the dev forgot to link the Photon Transform View component in the Observed Components of the Photon View component, that is not my case. While doing research on the problem I also posted on Stack Overflow and the officiel Photon forums but no luck. So I don’t really know what I did wrong here.
Both player instance have different ViewIDs and RPCs work from player to player.
Here is my console log when playing :