0
votes

I'm working in a car game with multiplayer. All works fine, master and clients are connected to the room fine. (I have been using Marco Polo tutorial) The problem is when I see other cars moving in the screen, the position updates by teleporting the cars. Appears and disappears all the time.

Part of my code:

PhotonNetwork.automaticallySyncScene = false;

public class CNPlayerManager : Photon.MonoBehaviour
{
...
 void FixedUpdate()
    {
        if (photonView.isMine)
        {
            //it works fine
        }
        else
        {
            transform.position= Vector3.Lerp(transform.position, this.correctPosition, Time.deltaTime * 5);
            transform.rotation= Quaternion.Lerp(transform.rotation, this.correctRotation, Time.deltaTime * 5);
        }
    }

public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
    if (stream.isWriting)
    {
            stream.SendNext(transform.position);
            stream.SendNext(transform.rotation);
        
    }
    else
    {
            this.correctPosition = (Vector3)stream.ReceiveNext();               //Line 100
            this.correctRotation = (Quaternion)stream.ReceiveNext();       //Line 101
        
    }
}

My PhotonView in my car prefab is like this:
(source: photonengine.com)
but in my Photon Version I have more options. In Owner I have "set at runtime" and "Fixed". And in "Observed components" I have 2 components, my car prefab and the script CNPlayerManager.

When I playing with 2 cars, In the first car I sometimes get this error: "IndexOutOfRangeException: Array index is out of range. PhotonStream.ReceiveNext ()..." In the line 100. In the second car I get the same.

Could you help me please?

1

1 Answers

0
votes

what do you mean when you say that you "observe the car prefab"? I think it is enough to just observe the CNPlayerManager script in this case since it handles the position and rotation synchronization.

However I would like to recommend you taking a look at the scripts the PUN package already contains, especially the PhotonTransformView script. This one does what you want to achieve and also provides some synchronization options you can check and see which one fits your needs best.

Please let us know if you still run into problems.