From 730456a83bb7e637aec5141999039a91fb268f06 Mon Sep 17 00:00:00 2001 From: in0finite Date: Wed, 4 May 2022 11:10:00 +0200 Subject: [PATCH] try to interpolate based on local time --- Assets/Scripts/Networking/TransformSyncer.cs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/Assets/Scripts/Networking/TransformSyncer.cs b/Assets/Scripts/Networking/TransformSyncer.cs index 911f5833..49b8b182 100644 --- a/Assets/Scripts/Networking/TransformSyncer.cs +++ b/Assets/Scripts/Networking/TransformSyncer.cs @@ -76,6 +76,9 @@ namespace SanAndreasUnity.Net // timestamp of server when this data was sent (batch timestamp) public double RemoteTimeStamp; + // estimated local time for this snapshot + public double EstimatedLocalTime; + public void Apply(Transform tr) { tr.localPosition = this.Position; @@ -204,6 +207,7 @@ namespace SanAndreasUnity.Net if (m_snapshotBuffer.Count == 0) { + syncData.EstimatedLocalTime = NetworkTime.localTime; m_snapshotBuffer.Enqueue(syncData); m_lastAddedSnapshot = syncData; return; @@ -215,6 +219,7 @@ namespace SanAndreasUnity.Net return; } + syncData.EstimatedLocalTime = m_lastAddedSnapshot.EstimatedLocalTime + (syncData.RemoteTimeStamp - m_lastAddedSnapshot.RemoteTimeStamp); m_snapshotBuffer.Enqueue(syncData); m_lastAddedSnapshot = syncData; } @@ -259,7 +264,7 @@ namespace SanAndreasUnity.Net if (null == m_snapshotBuffer || m_snapshotBuffer.Count == 0) return; - double currentNetworkTime = NetworkTime.time - m_parameters.snapshotLatency; + double currentNetworkTime = NetworkTime.localTime - m_parameters.snapshotLatency; // find higher and lower snapshots - those are snapshots that surround the 'currentNetworkTime' @@ -271,7 +276,7 @@ namespace SanAndreasUnity.Net bool foundFirstHigher = false; foreach (SyncData snapshot in m_snapshotBuffer) { - if (snapshot.RemoteTimeStamp >= currentNetworkTime) + if (snapshot.EstimatedLocalTime >= currentNetworkTime) { syncDataOfHigher = snapshot; if (isFirst) @@ -292,7 +297,7 @@ namespace SanAndreasUnity.Net } // interpolate between lower and higher syncdata - double ratio = (currentNetworkTime - syncDataOfLower.RemoteTimeStamp) / (syncDataOfHigher.RemoteTimeStamp - syncDataOfLower.RemoteTimeStamp); + double ratio = (currentNetworkTime - syncDataOfLower.EstimatedLocalTime) / (syncDataOfHigher.EstimatedLocalTime - syncDataOfLower.EstimatedLocalTime); if (double.IsNaN(ratio)) ratio = 1; ratio = Mathd.Clamp01(ratio); @@ -300,7 +305,7 @@ namespace SanAndreasUnity.Net this.Apply(interpolated); // remove old snapshots, but be careful not to remove the current lower snapshot - this.RemoveOldSnapshots(System.Math.Min(currentNetworkTime, syncDataOfLower.RemoteTimeStamp)); + this.RemoveOldSnapshots(System.Math.Min(currentNetworkTime, syncDataOfLower.EstimatedLocalTime)); } @@ -312,7 +317,7 @@ namespace SanAndreasUnity.Net break; SyncData syncData = m_snapshotBuffer.Peek(); - if (syncData.RemoteTimeStamp >= currentNetworkTime) + if (syncData.EstimatedLocalTime >= currentNetworkTime) break; m_snapshotBuffer.Dequeue();