Search code examples
c#unity-game-enginephotonphoton-pun

Using Photon in Unity not in sync with the players


I have recently started using Photon to create multiplayer games, my tutorial is linked here: https://www.youtube.com/watch?v=93SkbMpWCGo . As you can read from the title of this problem, the players movements are not syncing.

here if the code for my player:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using Photon.Pun;

public class playerControls : MonoBehaviour
{
    [SerializeField] private Rigidbody2D body;
    [SerializeField] public Transform bodyTransform;
    public float speed;
    private float horizontalInput;
    public PhotonView view;

    // Update is called once per frame
    void Update()
    {
        if(view.IsMine){
            if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.D))
            {
                bodyTransform.localScale = new Vector3(1.3f,1.3f,1.3f);
            }
            else if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A))
            {
                bodyTransform.localScale = new Vector3(-1.3f,1.3f,1.3f);
            }
            horizontalInput = Input.GetAxis("Horizontal");
            body.velocity = new Vector2(horizontalInput * (float)speed, body.velocity.y);
            if(Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.W)){
                body.velocity=new Vector2(body.velocity.x,6);
            }
        }
    }
}

I have also used the Photon Transform View, however it still does not seem to work.

Everything else works fine, you can move each player individually. You can join rooms. And the players spawn. The only problem however is just that the player sync does not work. Any feedback would be appreceated -Jake

Sidenote it is playable here: https://chartreusefinancialdifferences.jake-is-theis.repl.co/


Solution

  • PhotonTransformView is just a component that can be synced over the network. You also need PhotonView that does actual sync and drag-and-drop your PhotonTransformView to the Observed Components list. So basically the PhotonView is most important component for networking, PhotonTransformView is just a serializing/deserializing values from transform. If you add any class implementing IPunObservable you need to drag them to the Observed Components list to work