Skip to content

Commit

Permalink
fix: NetworkTime creates it's own frame time in 2019 (MirrorNetworkin…
Browse files Browse the repository at this point in the history
…g#3624)

Since we don't have double time there yet and the stopwatch will change during the frame which causes jitter and other artifacts
supersedes MirrorNetworking#3623
See also MirrorNetworking#2838
  • Loading branch information
imerr authored Oct 8, 2023
1 parent c312b6b commit 0c5eba7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
1 change: 1 addition & 0 deletions Assets/Mirror/Core/NetworkLoop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ static void NetworkEarlyUpdate()
// however, we only want to call NetworkServer/Client in play mode.
if (!Application.isPlaying) return;

NetworkTime.EarlyUpdate();
//Debug.Log($"NetworkEarlyUpdate {Time.time}");
NetworkServer.NetworkEarlyUpdate();
NetworkClient.NetworkEarlyUpdate();
Expand Down
14 changes: 11 additions & 3 deletions Assets/Mirror/Core/NetworkTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ public static double localTime
}
#else
// need stopwatch for older Unity versions, but it's quite slow.
// CAREFUL: unlike Time.time, this is not a FRAME time.
// it changes during the frame too.
// CAREFUL: unlike Time.time, the stopwatch time is not a FRAME time.
// it changes during the frame, so we have an extra step to "cache" it in EarlyUpdate.
static readonly Stopwatch stopwatch = new Stopwatch();
static NetworkTime() => stopwatch.Start();
public static double localTime => stopwatch.Elapsed.TotalSeconds;
static double localFrameTime;
public static double localTime => localFrameTime;
#endif

/// <summary>The time in seconds since the server started.</summary>
Expand Down Expand Up @@ -231,5 +232,12 @@ internal static void OnServerPong(NetworkConnectionToClient conn, NetworkPongMes
double newRtt = localTime - message.localTime;
conn._rtt.Add(newRtt);
}

internal static void EarlyUpdate()
{
#if !UNITY_2020_3_OR_NEWER
localFrameTime = stopwatch.Elapsed.TotalSeconds;
#endif
}
}
}

0 comments on commit 0c5eba7

Please sign in to comment.