Syncing position without Network Transform

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:

    public override bool OnSerialize(NetworkWriter writer, bool initialState) {
        Debug.Log("Write");
        writer.Write (transform.position);
        return true;
    }
    public override void OnDeserialize (NetworkReader reader, bool initialState) {
        var sposition = reader.ReadVector3 ();
        Debug.Log("Read: "+sposition);
        transform.position = sposition;
        return;
    }

(With this.SetDirtyBit (1); on local player)

But this doesn’t work. Also Syncvar doesnt do a thing also, but I guess thats maybe because only the server can update syncvar values?

Maybe I can try to send the data from client to server with the [Command] call, or would this be to slow if a player sends this like 5 times a second?

What is the best method?

Hi Royall,

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();
    }
}

Thx SuperNeon! This works perfectly! You only forgot to put [Command] above the CmdMove function :wink:

You’re welcome :smile:

The doc warns about sending commands every update.

I’ve been looking for a NetworkUpdate function that would be called on the same interval as the network interval.

In the meantime i coded a simple timer with Time.time.

Yup, I did the same thing:

sendRateTimer += Time.deltaTime;
            if(sendRateTimer > (1.0f/sendRate)) {
                    sendRateTimer = 0.0f;
                    CmdMove(playerCurrentPos, playerCurrentRot);
            }

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(Vector3 position) 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.

I’m doing the same, but without the array. Can I ask what difference having an array will make?

I’m using the array to interpolate the position.

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)

Thanks for sharing, I’m using this instead of the Network Transform which is really laggy!