Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Photon Transform View not syncing players position on both screen

Discussion in 'Connected Games' started by Monique_Dumont, Jun 4, 2020.

  1. Monique_Dumont

    Monique_Dumont

    Joined:
    Feb 18, 2020
    Posts:
    21
    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 : https://doc.photonengine.com/en-us/pun/current/demos-and-tutorials/pun-basics-tutorial/intro

    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) :

    Code (CSharp):
    1. using UnityEngine;
    2. using Photon.Pun;
    3. using Photon.Realtime;
    4.  
    5. public class Launch : MonoBehaviourPunCallbacks
    6. {
    7.     [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")]
    8.     [SerializeField]
    9.     private byte maxPlayersPerRoom = 4;
    10.     [Tooltip("The Ui Panel to let the user enter name, connect and play")]
    11.     [SerializeField]
    12.     private GameObject controlPanel;
    13.     [Tooltip("The UI Label to inform the user that the connection is in progress")]
    14.     [SerializeField]
    15.     private GameObject progressLabel;
    16.  
    17.     private readonly string gameVersion = "1";
    18.     private bool isConnecting;
    19.  
    20.     private void Awake()
    21.     {
    22.         PhotonNetwork.AutomaticallySyncScene = true;
    23.     }
    24.  
    25.     private void Start()
    26.     {
    27.         controlPanel.SetActive(true);
    28.         progressLabel.SetActive(false);
    29.     }
    30.  
    31.     public void Connect()
    32.     {
    33.         controlPanel.SetActive(false);
    34.         progressLabel.SetActive(true);
    35.  
    36.         if (PhotonNetwork.IsConnected)
    37.         {
    38.             PhotonNetwork.JoinRandomRoom();
    39.         }
    40.         else
    41.         {
    42.             isConnecting = PhotonNetwork.ConnectUsingSettings();
    43.             PhotonNetwork.ConnectUsingSettings();
    44.             PhotonNetwork.GameVersion = gameVersion;
    45.         }
    46.     }
    47.  
    48.     public override void OnConnectedToMaster()
    49.     {
    50.         Debug.Log("Launcher: OnConnectedToMaster() was called by PUN");
    51.  
    52.         if (isConnecting)
    53.         {
    54.             PhotonNetwork.JoinRandomRoom();
    55.             isConnecting = false;
    56.         }
    57.     }
    58.  
    59.     public override void OnDisconnected(DisconnectCause cause)
    60.     {
    61.         controlPanel.SetActive(true);
    62.         progressLabel.SetActive(false);
    63.         isConnecting = false;
    64.         Debug.LogWarningFormat("Launcher: OnDisconnected() was called by PUN with reason {0}", cause);
    65.     }
    66.  
    67.     public override void OnJoinRandomFailed(short returnCode, string message)
    68.     {
    69.         Debug.Log("Launcher:OnJoinRandomFailed() was called by PUN. No random room available, so we create one.\nCalling: PhotonNetwork.CreateRoom");
    70.         PhotonNetwork.CreateRoom(null, new RoomOptions {MaxPlayers = maxPlayersPerRoom });
    71.     }
    72.  
    73.     public override void OnJoinedRoom()
    74.     {
    75.         Debug.Log("Launcher: OnJoinedRoom() called by PUN. Now this client is in a room.");
    76.     }
    77.  
    78.     private void LoadArena()
    79.     {
    80.         if (!PhotonNetwork.IsMasterClient)
    81.         {
    82.             Debug.LogError("PhotonNetwork : Trying to Load a level but we are not the master Client");
    83.         }
    84.  
    85.         Debug.LogFormat("PhotonNetwork : Loading Arena");
    86.         PhotonNetwork.LoadLevel("SampleScene");
    87.     }
    88.  
    89.     public override void OnPlayerEnteredRoom(Player newPlayer)
    90.     {
    91.         Debug.LogFormat("OnPlayerEnteredRoom() {0}", newPlayer.NickName);
    92.  
    93.         if (PhotonNetwork.IsMasterClient && PhotonNetwork.CurrentRoom.PlayerCount == 2)
    94.         {
    95.             Debug.LogFormat("OnPlayerEnteredRoom IsMasterClient {0}", PhotonNetwork.IsMasterClient);
    96.  
    97.             LoadArena();
    98.         }
    99.     }
    100. }

    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) :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using Photon.Pun;
    6. using Photon.Realtime;
    7.  
    8. public class GameManager : MonoBehaviourPunCallbacks
    9. {
    10.     private Vector3 position;
    11.  
    12.     [Tooltip("The prefab to use for representing the player")]
    13.     public GameObject playerPrefab;
    14.  
    15.  
    16.     private void Start()
    17.     {
    18.         if (PhotonNetwork.IsMasterClient)
    19.         {
    20.             position = new Vector3(-4f, 0.5f, 9f);
    21.         }
    22.         else
    23.         {
    24.             position = new Vector3(4f, 0.5f, 9f);
    25.         }
    26.  
    27.         if (playerPrefab == null)
    28.         {
    29.             Debug.LogError("<Color=Red><a>Missing</a></Color> playerPrefab Reference. Please set it up in GameObject 'Game Manager'", this);
    30.         }
    31.         else
    32.         {
    33.             PhotonNetwork.Instantiate(this.playerPrefab.name, position, Quaternion.identity, 0);
    34.         }
    35.     }
    36.  
    37.     public override void OnLeftRoom()
    38.     {
    39.         SceneManager.LoadScene(0);
    40.     }
    41.  
    42.  
    43.  
    44.     public void LeaveRoom()
    45.     {
    46.         PhotonNetwork.LeaveRoom();
    47.     }
    48.  
    49.  
    50.  
    51.     public override void OnPlayerLeftRoom(Player otherPlayer)
    52.     {
    53.         Debug.LogFormat("OnPlayerLeftRoom() {0}", otherPlayer.NickName);
    54.  
    55.         if (PhotonNetwork.IsMasterClient)
    56.         {
    57.             Debug.LogFormat("OnPlayerLeftRoom IsMasterClient {0}", PhotonNetwork.IsMasterClient);
    58.  
    59.             LeaveRoom();
    60.         }
    61.     }
    62. }
    63.  


    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 :
     
  2. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    378
    Hi,

    it's likely because there is a script on your player that override the position of the player, for example, the component that is responsible for moving the player may still be running when it should not, make sure you disable any user input on instances of players where the photonview .isMine bool is not true.

    Bye,

    Jean
     
  3. Monique_Dumont

    Monique_Dumont

    Joined:
    Feb 18, 2020
    Posts:
    21
    Hi,

    Thank you for your response. I feel so bad now because I indeed checked photonView.IsMine before accepting input, but I accepted input if photonView.IsMine was false, and I overlooked it each time I reviewed my code.

    I just changed false to true and now it's working perfectly. I facepalmed so hard.

    Anyway thank you so much, you made my day, really.
     
    tobiass likes this.
  4. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    378
    Hi,

    no worries!

    Bye,

    Jean
     
    Llama_w_2Ls likes this.
  5. Llama_w_2Ls

    Llama_w_2Ls

    Joined:
    May 16, 2020
    Posts:
    6
    Dude, youre a legend
     
    tobiass likes this.
unityunity