Skip to content

Commit 08f261c

Browse files
update
Time systems related updates
1 parent b784e14 commit 08f261c

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

com.unity.netcode.gameobjects/Runtime/Timing/NetworkTime.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,19 @@ public struct NetworkTime
4545
public double FixedTime => m_CachedTick * m_TickInterval;
4646

4747
/// <summary>
48-
/// Gets the fixed delta time. This value is based on the <see cref="TickRate"/> and stays constant.
49-
/// Similar to <see cref="Time.fixedUnscaledTime"/> There is no equivalent to <see cref="Time.deltaTime"/>.
48+
/// Gets the fixed delta time. This value is calculated by dividing 1.0 by the <see cref="TickRate"/> and stays constant.
5049
/// </summary>
50+
/// <remarks>
51+
/// This could result in a potential floating point precision variance on different systems. <br />
52+
/// See <see href="FixedDeltaTime"></see> for a more precise value.
53+
/// </remarks>
5154
public float FixedDeltaTime => (float)m_TickInterval;
5255

56+
/// <summary>
57+
/// Gets the fixed delta time as a double. This value is calculated by dividing 1.0 by the <see cref="TickRate"/> and stays constant.
58+
/// </summary>
59+
public double FixedDeltaTimeAsDouble => m_TickInterval;
60+
5361
/// <summary>
5462
/// Gets the amount of network ticks which have passed until reaching the current time value.
5563
/// </summary>
@@ -70,7 +78,7 @@ public NetworkTime(uint tickRate)
7078
Assert.IsTrue(tickRate > 0, "Tickrate must be a positive value.");
7179

7280
m_TickRate = tickRate;
73-
m_TickInterval = 1f / m_TickRate; // potential floating point precision issue, could result in different interval on different machines
81+
m_TickInterval = 1.0 / m_TickRate;
7482
m_CachedTickOffset = 0;
7583
m_CachedTick = 0;
7684
m_TimeSec = 0;

com.unity.netcode.gameobjects/Runtime/Timing/NetworkTimeSystem.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using Unity.Profiling;
3+
using UnityEngine;
34

45
namespace Unity.Netcode
56
{
@@ -35,7 +36,7 @@ public class NetworkTimeSystem
3536
#if DEVELOPMENT_BUILD || UNITY_EDITOR
3637
private static ProfilerMarker s_SyncTime = new ProfilerMarker($"{nameof(NetworkManager)}.SyncTime");
3738
#endif
38-
39+
private double m_PreviousTimeSec;
3940
private double m_TimeSec;
4041
private double m_CurrentLocalTimeOffset;
4142
private double m_DesiredLocalTimeOffset;
@@ -75,8 +76,20 @@ public class NetworkTimeSystem
7576
/// </summary>
7677
public double ServerTime => m_TimeSec + m_CurrentServerTimeOffset;
7778

79+
private float m_TickLatencyAverage = 2.0f;
80+
81+
/// <summary>
82+
/// The averaged latency in network ticks between a client and server.
83+
/// </summary>
84+
/// <remarks>
85+
/// For a distributed authority network topology, this latency is between
86+
/// the client and the distributed authority service instance.
87+
/// </remarks>
88+
public int TickLatency = 2;
89+
7890
internal double LastSyncedServerTimeSec { get; private set; }
7991
internal double LastSyncedRttSec { get; private set; }
92+
internal double LastSyncedHalfRttSec { get; private set; }
8093

8194
private NetworkConnectionManager m_ConnectionManager;
8295
private NetworkTransport m_NetworkTransport;
@@ -101,6 +114,7 @@ public NetworkTimeSystem(double localBufferSec, double serverBufferSec = k_Defau
101114
ServerBufferSec = serverBufferSec;
102115
HardResetThresholdSec = hardResetThresholdSec;
103116
AdjustmentRatio = adjustmentRatio;
117+
m_TickLatencyAverage = 2;
104118
}
105119

106120
/// <summary>
@@ -203,7 +217,14 @@ public static NetworkTimeSystem ServerTimeSystem()
203217
/// <returns>True if a hard reset of the time system occurred due to large time offset differences. False if normal time advancement occurred</returns>
204218
public bool Advance(double deltaTimeSec)
205219
{
220+
m_PreviousTimeSec = m_TimeSec;
206221
m_TimeSec += deltaTimeSec;
222+
// TODO: For client-server, we need a latency message sent by clients to tell us their tick latency
223+
if (LastSyncedRttSec > 0.0f)
224+
{
225+
m_TickLatencyAverage = Mathf.Lerp(m_TickLatencyAverage, (float)((LastSyncedRttSec + deltaTimeSec) / m_NetworkTickSystem.ServerTime.FixedDeltaTimeAsDouble), (float)deltaTimeSec);
226+
TickLatency = (int)Mathf.Max(1.0f, Mathf.Round(m_TickLatencyAverage)) + 1;
227+
}
207228

208229
if (Math.Abs(m_DesiredLocalTimeOffset - m_CurrentLocalTimeOffset) > HardResetThresholdSec || Math.Abs(m_DesiredServerTimeOffset - m_CurrentServerTimeOffset) > HardResetThresholdSec)
209230
{
@@ -243,19 +264,18 @@ public void Reset(double serverTimeSec, double rttSec)
243264
public void Sync(double serverTimeSec, double rttSec)
244265
{
245266
LastSyncedRttSec = rttSec;
267+
LastSyncedHalfRttSec = (rttSec * 0.5d);
246268
LastSyncedServerTimeSec = serverTimeSec;
247269

248270
var timeDif = serverTimeSec - m_TimeSec;
249271

250272
m_DesiredServerTimeOffset = timeDif - ServerBufferSec;
251273
// We adjust our desired local time offset to be half RTT since the delivery of
252274
// the TimeSyncMessage should only take half of the RTT time (legacy was using 1 full RTT)
253-
m_DesiredLocalTimeOffset = timeDif + (rttSec * 0.5d) + LocalBufferSec;
275+
m_DesiredLocalTimeOffset = timeDif + LastSyncedHalfRttSec + LocalBufferSec;
254276
}
255277

256278
internal double ServerTimeOffset => m_DesiredServerTimeOffset;
257279
internal double LocalTimeOffset => m_DesiredLocalTimeOffset;
258-
259-
internal double RawTime => m_TimeSec;
260280
}
261281
}

0 commit comments

Comments
 (0)