Search Unity

Third Party Photon Transform View not syncing players position on both screen

Discussion in 'Multiplayer' started by Monique_Dumont, Jun 4, 2020.

  1. Monique_Dumont

    Monique_Dumont

    Joined:
    Feb 18, 2020
    Posts:
    41
    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:
    429
    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:
    41
    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:
    429
    Hi,

    no worries!

    Bye,

    Jean
     
    Llama_w_2Ls likes this.
  5. Llama_w_2Ls

    Llama_w_2Ls

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

    nigammonu58

    Joined:
    Apr 1, 2022
    Posts:
    1
    I am facing the same issue. I have added code to move player for photonView.IsMine true. Rotation is syncing properly but position of other player does not change. Plz guide how to fix this issue
     

    Attached Files:

  7. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    429
    Hi,

    you have a character controller component, are you properly disable that when photonView.IsMine is false?

    Bye.

    Jean
     
  8. back2back7524

    back2back7524

    Joined:
    Jul 6, 2022
    Posts:
    1
    Hello everyone!!

    I'm having same problem I guess.. I'm using Photon View in my Player Object.
    My problem is, when I run the program, I see other player screen and other player sees my screen..
    But control works fine... It's like I am viewing other players screen yet controlling my character.

    (Extremely sorry if I said anything dumb or wrong, newbie here)

    I'll give my code below
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using Photon.Pun;
    6.  
    7. public class PlayerController : MonoBehaviour
    8. {
    9.     public Player playerInput;
    10.     private CharacterController controller;
    11.     private Transform cameraMain;
    12.     private Vector3 playerVelocity;
    13.     private bool groundedPlayer;
    14.     private Transform child;
    15.     PhotonView view;
    16.  
    17.     [SerializeField]
    18.     private float playerSpeed = 2.0f;
    19.     [SerializeField]
    20.     private float gravityValue = -9.81f;
    21.     [SerializeField]
    22.     private float rotationSpeed = 4f;
    23.  
    24.     public void Awake()
    25.     {
    26.         playerInput = new Player();
    27.         controller = GetComponent<CharacterController>();
    28.     }
    29.     public void OnEnable()
    30.     {
    31.         playerInput.Enable();
    32.     }
    33.     public void OnDisable()
    34.     {
    35.         playerInput.Disable();
    36.     }
    37.  
    38.     private void Start()
    39.     {
    40.         cameraMain = Camera.main.transform;
    41.         child = transform.GetChild(0).transform;
    42.         view = GetComponent<PhotonView>();
    43.     }
    44.  
    45.     void Update()
    46.     {
    47.         if (view.IsMine)
    48.         {
    49.             groundedPlayer = controller.isGrounded;
    50.             if (groundedPlayer && playerVelocity.y < 0)
    51.             {
    52.                 playerVelocity.y = 0f;
    53.             }
    54.  
    55.             Vector2 movementInput = playerInput.PlayerMain.Move.ReadValue<Vector2>();
    56.             Vector3 move = (cameraMain.forward * movementInput.y + cameraMain.right * movementInput.x);
    57.             move.y = 0f;
    58.             controller.Move(move * Time.deltaTime * playerSpeed);
    59.  
    60.  
    61.             playerVelocity.y += gravityValue * Time.deltaTime;
    62.             controller.Move(playerVelocity * Time.deltaTime);
    63.  
    64.             if (movementInput != Vector2.zero)
    65.             {
    66.                 Quaternion rotation = Quaternion.Euler(new Vector3(child.localEulerAngles.x, cameraMain.localEulerAngles.y, child.localEulerAngles.z));
    67.                 child.rotation = Quaternion.Lerp(child.rotation, rotation, Time.deltaTime * rotationSpeed);
    68.             }
    69.         }
    70.     }
    71. }
    72.  
     
  9. vandermky474

    vandermky474

    Joined:
    Feb 28, 2021
    Posts:
    10
    hello could you explain that a bit better I'm having similar problem were I'm trying to make a vr gorilla tag fan game but the moments are not syncing. this is the video I followed
     
  10. arabymohammedyoussef123

    arabymohammedyoussef123

    Joined:
    Jul 17, 2023
    Posts:
    5
    Hey I have same problem here, the editor is syncing other players fine but other players can't sync each other I don't know what is the problem I tried to host on different devices not editor but still the editor is the only one that is syncing other players this is the player movement script:

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using Photon.Pun;
    4.  
    5. public class FirstPersonMovement : MonoBehaviourPun
    6. {
    7.     public float speed = 5;
    8.  
    9.     [Header("Running")]
    10.     public bool canRun = true;
    11.     public bool IsRunning { get; private set; }
    12.     public float runSpeed = 9;
    13.     public KeyCode runningKey = KeyCode.LeftShift;
    14.  
    15.     Rigidbody rigidbody;
    16.     /// <summary> Functions to override movement speed. Will use the last added override. </summary>
    17.     public List<System.Func<float>> speedOverrides = new List<System.Func<float>>();
    18.  
    19.  
    20.  
    21.     void Awake()
    22.     {
    23.         if (!photonView.IsMine) return;
    24.         // Get the rigidbody on this.
    25.         rigidbody = GetComponent<Rigidbody>();
    26.     }
    27.  
    28.     void FixedUpdate()
    29.     {
    30.         if (!photonView.IsMine) return;
    31.         // Update IsRunning from input.
    32.         IsRunning = canRun && Input.GetKey(runningKey);
    33.  
    34.         // Get targetMovingSpeed.
    35.         float targetMovingSpeed = IsRunning ? runSpeed : speed;
    36.         if (speedOverrides.Count > 0)
    37.         {
    38.             targetMovingSpeed = speedOverrides[speedOverrides.Count - 1]();
    39.         }
    40.  
    41.         // Get targetVelocity from input.
    42.         Vector2 targetVelocity =new Vector2( Input.GetAxis("Horizontal") * targetMovingSpeed, Input.GetAxis("Vertical") * targetMovingSpeed);
    43.  
    44.         // Apply movement.
    45.         rigidbody.velocity = transform.rotation * new Vector3(targetVelocity.x, rigidbody.velocity.y, targetVelocity.y);
    46.     }
    47. }
    and I made sure that the player prefab got the PhotonView and the PhotonTransformView