So as I want to look up into custom interpolation but before that I am trying to figure out the best way of syncing raw position and rotation data. I tried the following:
As you said, SyncVar is replicated from the server value.
OnSerialize is called on the server.
OnDeserialize is called on the client.
You can use OnDeserialize to update the replicated player (when isLocalPlayer is false).
You can try this:
public class NetPlayer : NetworkBehaviour
{
void Update()
{
if (isLocalPlayer)
{
// [...] code to update the position of the player.
// send to the server my new position.
CmdMove(transform.position);
}
}
[Command]
void CmdMove(Vector3 position)
{
// we trust the player :)
transform.position = position;
SetDirtyBit(1u);
}
public override bool OnSerialize(NetworkWriter writer, bool initialState)
{
writer.Write(transform.position);
return true;
}
public override void OnDeserialize(NetworkReader reader, bool initialState)
{
if(isLocalPlayer)
{
return;
}
transform.position = reader.ReadVector3();
}
}
I’m confused, are the CmdMove+Update and the OnSerialize+OnDeserialize two separate examples? Because why do I need to write and set transform.position in two different places if these are part of the same example?
CmdMove(transform.position); is the client sending position to the server.
[Command] void CmdMove(Vector3position) is on the server where it receives the pos from the client.
OnSerialize Is runned by the server sending the position to the other players.
OnDeserialize Is runned by the other clients so they receive the position
To give more solutions : I use a simple SyncVar with a hook instead of OnSerialize /OnDeserialize. The hook is adding positions to an array which is then used by an interpolation system.
So instead of interpolating the current position to the latest position received from the server, you interpolate to the oldest position and then once you reach it you remove it from the array? Do you speed up the interpolation depending on the number of positions that need interpolating in order to avoid lagging behind?
Yes and no. if the position is too old it’s not used and removed. The interpolation then interpolates only recent enough positions. (it’s not based on speed but more on the network update rate and current delta time)