From 167d49e365a0e302e6317dbc68eb4d00f75eab4b Mon Sep 17 00:00:00 2001 From: JesusLuvsYooh Date: Sat, 6 Jul 2024 19:33:29 +0100 Subject: [PATCH] Draft - Update, Late, Fixed. Needs renaming, and better comments/tooltip. --- .../NetworkTransform/NetworkTransformBase.cs | 6 +++++ .../NetworkTransformReliable.cs | 25 +++++++++++++++---- .../NetworkTransformUnreliable.cs | 25 +++++++++++++++---- 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs b/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs index ab00ba22f..fe121e0cb 100644 --- a/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs +++ b/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs @@ -23,6 +23,7 @@ namespace Mirror { public enum CoordinateSpace { Local, World } + public enum UpdateMethod { Update, LateUpdate, FixedUpdate } public abstract class NetworkTransformBase : NetworkBehaviour { @@ -92,6 +93,11 @@ public abstract class NetworkTransformBase : NetworkBehaviour protected double timeStampAdjustment => NetworkServer.sendInterval * (sendIntervalMultiplier - 1); protected double offset => timelineOffset ? NetworkServer.sendInterval * sendIntervalMultiplier : 0; + // CoordinateSpace /////////////////////////////////////////////////////////// + [Header("Update Method")] + [Tooltip("Update by default. Try a different method when having problems with Physics or Animations.")] + public UpdateMethod updateMethod = UpdateMethod.Update; + // debugging /////////////////////////////////////////////////////////// [Header("Debug")] public bool showGizmos; diff --git a/Assets/Mirror/Components/NetworkTransform/NetworkTransformReliable.cs b/Assets/Mirror/Components/NetworkTransform/NetworkTransformReliable.cs index fd87ff2c5..829bacd90 100644 --- a/Assets/Mirror/Components/NetworkTransform/NetworkTransformReliable.cs +++ b/Assets/Mirror/Components/NetworkTransform/NetworkTransformReliable.cs @@ -47,15 +47,15 @@ public class NetworkTransformReliable : NetworkTransformBase // update ////////////////////////////////////////////////////////////// void Update() { - // if server then always sync to others. - if (isServer) UpdateServer(); - // 'else if' because host mode shouldn't send anything to server. - // it is the server. don't overwrite anything there. - else if (isClient) UpdateClient(); + if (updateMethod == UpdateMethod.Update) + UpdateCall(); } void LateUpdate() { + if (updateMethod == UpdateMethod.LateUpdate) + UpdateCall(); + // set dirty to trigger OnSerialize. either always, or only if changed. // It has to be checked in LateUpdate() for onlySyncOnChange to avoid // the possibility of Update() running first before the object's movement @@ -70,6 +70,21 @@ void LateUpdate() } } + void FixedUpdate() + { + if (updateMethod == UpdateMethod.FixedUpdate) + UpdateCall(); + } + + void UpdateCall() + { + // if server then always sync to others. + if (isServer) UpdateServer(); + // 'else if' because host mode shouldn't send anything to server. + // it is the server. don't overwrite anything there. + else if (isClient) UpdateClient(); + } + protected virtual void UpdateServer() { // apply buffered snapshots IF client authority diff --git a/Assets/Mirror/Components/NetworkTransform/NetworkTransformUnreliable.cs b/Assets/Mirror/Components/NetworkTransform/NetworkTransformUnreliable.cs index ab3b2d7b7..ece19552a 100644 --- a/Assets/Mirror/Components/NetworkTransform/NetworkTransformUnreliable.cs +++ b/Assets/Mirror/Components/NetworkTransform/NetworkTransformUnreliable.cs @@ -36,11 +36,8 @@ public class NetworkTransformUnreliable : NetworkTransformBase // Update applies interpolation void Update() { - if (isServer) UpdateServerInterpolation(); - // for all other clients (and for local player if !authority), - // we need to apply snapshots from the buffer. - // 'else if' because host mode shouldn't interpolate client - else if (isClient && !IsClientWithAuthority) UpdateClientInterpolation(); + if (updateMethod == UpdateMethod.Update) + UpdateCall(); } // LateUpdate broadcasts. @@ -50,6 +47,9 @@ void Update() // this could cause visible jitter. void LateUpdate() { + if (updateMethod == UpdateMethod.LateUpdate) + UpdateCall(); + // if server then always sync to others. if (isServer) UpdateServerBroadcast(); // client authority, and local player (= allowed to move myself)? @@ -58,6 +58,21 @@ void LateUpdate() else if (isClient && IsClientWithAuthority) UpdateClientBroadcast(); } + void FixedUpdate() + { + if (updateMethod == UpdateMethod.FixedUpdate) + UpdateCall(); + } + + void UpdateCall() + { + if (isServer) UpdateServerInterpolation(); + // for all other clients (and for local player if !authority), + // we need to apply snapshots from the buffer. + // 'else if' because host mode shouldn't interpolate client + else if (isClient && !IsClientWithAuthority) UpdateClientInterpolation(); + } + protected virtual void CheckLastSendTime() { // We check interval every frame, and then send if interval is reached.