Search Unity

Photon - position interpolation

Discussion in 'Connected Games' started by Cjreek, Jul 1, 2014.

  1. Cjreek

    Cjreek

    Joined:
    Apr 23, 2013
    Posts:
    33
    Hi,

    I'm new to Unity- and Photon-Networking and I try to sync the space ships of my two test players and
    interpolate between the received positions.

    If I don't set my transform position directly to the last received position (after interpolating most of the distance in my update method) when receiving new data (1) the ships move really smooth but they don't quite reach their destination. So if I only press "forward" once on my first client the space ship on my second client moves smoothly as the original space ship of client 1 but it doesn't go that far. it stops quite a few pixels before its destination.

    If I set the transforms position to the last received position when receiving new data (1) the space ship does little jumps (which is expected considering the outcome when I'm not doing it).

    I guess my lerps aren't 100% as they're supposed to be?
    Can somebody tell me what I'm doing wrong?

    PS: I'm running both clients on the same machine so this whole thing propbably isn't affected by (real) latency.

    Code (csharp):
    1. public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    2. {
    3.     if (stream.isReading)
    4.     {
    5.         if(!view.isMine)
    6.         {
    7.             transform.position = trueLoc;  // <---- (1)
    8.             transform.rotation = trueRot;
    9.        
    10.             trueLoc = (Vector3)stream.ReceiveNext();
    11.             trueRot = (Quaternion)stream.ReceiveNext();
    12.             trueSpeed = (Vector2)stream.ReceiveNext();
    13.             trueAngularSpeed = (float)stream.ReceiveNext();
    14.             timestamp = (double)stream.ReceiveNext();
    15.         }
    16.     }
    17.     else
    18.     {
    19.         if(view.isMine)
    20.         {
    21.             stream.SendNext(transform.position);
    22.             stream.SendNext(transform.rotation);
    23.             stream.SendNext(rigidbody2D.velocity);
    24.             stream.SendNext(transform.rotation.eulerAngles.z - lastRot);
    25.             stream.SendNext(PhotonNetwork.time);
    26.            
    27.             lastRot = transform.rotation.eulerAngles.z;
    28.         }
    29.     }
    30. }
    31.  
    32. void Update ()
    33. {
    34.     if (!view.isMine)
    35.     {      
    36.         rigidbody2D.velocity = Vector2.zero; // otherwise this caused problems
    37.  
    38.         transform.position = Vector3.Lerp(transform.position, trueLoc,
    39.             trueSpeed.magnitude * Time.deltaTime);
    40.         transform.rotation = Quaternion.Lerp(transform.rotation, trueRot,
    41.             trueAngularSpeed * Time.deltaTime);        
    42.     }
    43. }
     
  2. Serdnad

    Serdnad

    Joined:
    Jan 9, 2013
    Posts:
    14
    The fact that both instances are being run from the same computer doesn't mean that there's no latency. The information still has to go to a server and back. The only case where you'd get basicly no latency would be if you ran the server on that same computer.

    Regardless, that shouldn't affect it. I'm not sure here, just learning about this stuff myself, but I believe if you move the first 2 lines under "if(!view.isMine)" down, it should get that last movement.

    Code (CSharp):
    1. if(!view.isMine)
    2.         {      
    3.             trueLoc = (Vector3)stream.ReceiveNext();
    4.             trueRot = (Quaternion)stream.ReceiveNext();
    5.             trueSpeed = (Vector2)stream.ReceiveNext();
    6.             trueAngularSpeed = (float)stream.ReceiveNext();
    7.             timestamp = (double)stream.ReceiveNext();
    8.  
    9.             transform.position = trueLoc;
    10.             transform.rotation = trueRot;
    11.         }
    If it does work, then it's because now you're actually using the data from the last update. OnPhotonSerialize only fires if something has changed. Before, you were setting the final position to the second last update you got. Did that make sense?
     
  3. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
  4. Cjreek

    Cjreek

    Joined:
    Apr 23, 2013
    Posts:
    33
unityunity