Search Unity

Different positions

Discussion in 'Multiplayer' started by dannyhodge, Oct 31, 2014.

  1. dannyhodge

    dannyhodge

    Joined:
    Mar 14, 2013
    Posts:
    30
    Hey guys,
    So I'm using Photon for a coop 2D shmup, and I've (finally) managed to smooth out the movement. But another problem that has plagued me for a while is that the positions don't quite match up for the players. For example, on one version I have running I am ahead, whereas on the other they are ahead. I am wanting to implement colliders, which is pretty confusing when you don't know where the other player is.

    Any hint as to what I'm doing wrong would be great. This is the code for the players being networked (its pretty standard):
    Code (CSharp):
    1. void Update () {
    2.    
    3.     if(photonView.isMine) {
    4.        
    5.  
    6.         }
    7.         else {
    8.             transform.position = Vector3.Lerp(transform.position, realPosition, Time.deltaTime * 5);
    9.  
    10.         }
    11.     }
    12.  
    13.     public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) {
    14.         if(stream.isWriting) {
    15.              stream.SendNext (transform.position);
    16.  
    17.         }
    18.  
    19.         else {
    20.             realPosition = (Vector3)stream.ReceiveNext();
    21.         }
    22.     }

    I am also moving a camera over the network, but that has an almost identical script.
    Currently, everything is on Unreliable on change. Changing that didn't seem to have any effect on the outcome. Also, every window i view the game on is the same resolution.

    Thanks a lot,
    Danny
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,072
    The cause for your problem is network lag. When you do something and send an update to the others, it will take a moment until they know what you did.
    It can't be avoided, so you can only work around it.
    You can try to delay actions locally by a fixed amount of time but still send it immediately when the user gave some order or pressed some keys/buttons. This way, you wait a few milliseconds until the others could execute your action simultaneously.
    You can check how much time passed since the action was done and do something on the receiving end to catch up.