Lag when using either the Smooth Sync Movement or NetworkCharacter script. How do I get rid of lag?

Options
I'm trying to eliminate the lag or at least smooth the movements so the lag is not visible but I'm having difficulty. I've played around with the settings on the PhotonView and Photon Transform View as well as the SmoothSyncMovement script below that I attached to my player object but I've had no luck with reducing the lag.

I'm using the Photon Cloud server and not an on-premise local server.
The player game objects that I'm trying to sync across the network are also using Rigidbody.Addforce for player movement. Could this be a factor causing the lag?

What am I missing and doing wrong?

Thanks community,
Sfoxx28

[RequireComponent(typeof(PhotonView))]
public class SmoothSyncMovement : Photon.MonoBehaviour, IPunObservable
{
public float SmoothingDelay = 5;
public void Awake()
{
bool observed = false;
foreach (Component observedComponent in this.photonView.ObservedComponents)
{
if (observedComponent == this)
{
observed = true;
break;
}
}
if (!observed)
{
Debug.LogWarning(this + " is not observed by this object's photonView! OnPhotonSerializeView() in this class won't be used.");
}
}

public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
if (photonView.isMine)
{
//We own this player: send the others our data
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
}
}
else
{
if (!photonView.isMine)
{
//Network player, receive data
correctPlayerPos = (Vector3)stream.ReceiveNext();
correctPlayerRot = (Quaternion)stream.ReceiveNext();
}
}
}

private Vector3 correctPlayerPos = Vector3.zero; //We lerp towards this
private Quaternion correctPlayerRot = Quaternion.identity; //We lerp towards this

public void Update()
{
if (!photonView.isMine)
{
//Update remote player (smooth this, this looks good, at the cost of some accuracy)
transform.position = Vector3.Lerp(transform.position, correctPlayerPos, Time.deltaTime * this.SmoothingDelay);
transform.rotation = Quaternion.Lerp(transform.rotation, correctPlayerRot, Time.deltaTime * this.SmoothingDelay);
}
}

}

Comments

  • Hi @Sfoxx28,

    without any modification this is 'normal' behaviour and caused by the delay the message needs to 'travel' from one client across the server to another client. If you want to you can take a look at the discussion here which is about the same topic.

    In short: the sender can send an additional information which can be used by the receiver in order to try to predict the movement of the object. This way you won't get rid of lag either, but you can lower its occurrence. A documentation page about this topic will be available in the next days / weeks, there is currently no exact ETA about this.

    If you have further questions, please feel free to ask.