- Home /
Question by
lukachelidze · Aug 09, 2018 at 09:21 PM ·
networkingmultiplayertimesynchronization
How to synchronize Datetime from server to client?
I need to know server time on my client so I try to guess difference between client and server times by this code, however I'm getting different values for time difference on every update, plus I'm running it on localhost and there shouldn't be any difference. Another thing that looks strange is that lag is about 25ms, as I know it is supposed to be less than 5ms on localhost.Any suggestions?
[Command]
private void CmdPing()
{
TargetPing(connectionToClient, DateTime.Now.Ticks);
}
[TargetRpc]
private void TargetPing(NetworkConnection target, long serverTime)
{
_pingReceiveTime = DateTime.Now;
_lag = new TimeSpan((_pingReceiveTime - _pingSendTime).Ticks / 2);
ServerTimeDifferenceTicks = serverTime - DateTime.Now.Ticks - _lag.Ticks;
}
private IEnumerator Pinger()
{
while (true)
{
_pingSendTime = DateTime.Now;
CmdPing();
yield return new WaitForSeconds(1);
}
}
Comment