[Solved] Simple State Sync. Script Not Working

Options
roboJ
roboJ
Hi All, I'm making my first multiplayer game using PUN and I've run into an issue which I'm finding very confusing. I've already got the position of the two players working by simply having the PhotonView observe the players transform, however the latency issues leave a little to be desired. I've wrote a simple state sync script based on the tutorial here http://www.paladinstudios.com/2014/05/08/how-to-create-an-online-multiplayer-game-with-photon-unity-networking/#comment-2519. Here is the code in my Player script

[code2=csharp]void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info){
if(stream.isWriting){
stream.SendNext(transform.position);
// stream.SendNext(rigidbody.velocity);
}
else if (stream.isReading){
Vector3 syncEndPos = (Vector3) stream.ReceiveNext();
Vector3 syncStartPos = transform.position;

syncTime = 0f;
syncDelay = Time.time - lastSyncTime;
lastSyncTime = Time.time;
}
}
//Method called on the players that this client does not control
private void SyncMovement(){
syncTime += Time.deltaTime;
transform.position = Vector3.Lerp(syncStartPos,syncEndPos, syncTime / syncDelay);
// Debug.Log(transform.position);
}
void Update () {
//Movement and network syncronzation
if((gameObject.GetPhotonView().isMine || !PhotonNetwork.connected) && !_stunned)
ProcessMovement();
else if(!gameObject.GetPhotonView().isMine){
SyncMovement();
}
}[/code2]

And the reuslt is: The player's position doesn't update at all across the other clients.
Here's where things get interesting however, having my OnSerialize log the syncEndPos when it reads it shows very clearly that it is reading the updated position. However, by the time I get to the SyncMovement method, the syncEndPos has been overwritten back to the intial value so the player is never updated.
Any ideas how to fix this? So far I've tried only Sending if the photonview.isMine is true just in case the value was being overwritten on the other client where no one had moved but that didn't work. I'm totally stuck, my code is nearly identical to that in the tutorial.

I should note that I'm on Windows7 and using Unity 4.6 beta (latest stable version, just downloaded today)

Comments

  • roboJ
    Options
    So I realized my immensely stupid error, I was declaring syncStartPos and syncEndPos as local variables within my serialize method and was using my class variables in my lerp calculation, leading to the discrepancy :oops:. Sometimes you just need a good night's sleep :lol:
  • Tobias
    Options
    Hehe. Then it's good you got some rest (and found the bug).
    Thanks for the update.