Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Timer network behaviour. #134

Open
nisathnasar opened this issue Mar 7, 2021 · 2 comments
Open

Make Timer network behaviour. #134

nisathnasar opened this issue Mar 7, 2021 · 2 comments

Comments

@nisathnasar
Copy link
Member

As timer is client based, any differences in latencies causes differences in starting time and in ending time. Meaning some get to play when others haven't even begun or already finished the game.

@hsravat-4590
Copy link
Member

Differences in latency would mean that the times are always not going to be synchronised

@hsravat-4590
Copy link
Member

This might improve time sync performance but I assume there will still be issues if latency is bad:

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
 
public class NetworkPlayer : NetworkBehaviour
{
    public bool isNetworkTimeSynced = false;
 
    // timestamp received from server
    private int networkTimestamp;
 
    // server to client delay
    private int networkTimestampDelayMS;
 
    // when did we receive timestamp from server
    private float timeReceived;
 
    protected virtual void Start()
    {
        if (isLocalPlayer)
        {
            CmdRequestTime();
        }
    }
 
    [Command]
    private void CmdRequestTime()
    {
        int timestamp = NetworkTransport.GetNetworkTimestamp();
        RpcNetworkTimestamp(timestamp);
    }
 
    [ClientRpc]
    private void RpcNetworkTimestamp(int timestamp)
    {
        isNetworkTimeSynced = true;
        networkTimestamp = timestamp;
        timeReceived = Time.time;
 
        // if client is a host, assume that there is 0 delay
        if (isServer)
        {
            networkTimestampDelayMS = 0;
        }
        else
        {
            byte error;
            networkTimestampDelayMS = NetworkTransport.GetRemoteDelayTimeMS(
                NetworkManager.singleton.client.connection.hostId,
                NetworkManager.singleton.client.connection.connectionId,
                timestamp,
                out error);
        }
    }
 
    public float GetServerTime()
    {
        return networkTimestamp + (networkTimestampDelayMS / 1000f) + (Time.time - timeReceived);
    }
}

The above is Unet code so it will need refactoring to Mirror

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants