From e0e262678b2a3c7c015141a82a668f60a3a3000b Mon Sep 17 00:00:00 2001 From: JesusLuvsYooh <57072365+JesusLuvsYooh@users.noreply.github.com> Date: Thu, 16 Feb 2023 03:19:02 +0000 Subject: [PATCH] feature: NetworkTransform interpolation can be disabled (#3378) * Interpolation off bools for NetworkTransform. Gives a snap-like effect to position, rotation and scaling. * Update Assets/Mirror/Components/NetworkTransformBase.cs * Update NetworkTransformBase.cs --------- Co-authored-by: mischa <16416509+vis2k@users.noreply.github.com> --- .../Mirror/Components/NetworkTransformBase.cs | 26 +++++++++++++++---- .../NetworkTransformReliable.cs | 4 +-- .../NetworkTransform.cs | 4 +-- .../Tests/Editor/NetworkTransform2kTests.cs | 2 +- 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/Assets/Mirror/Components/NetworkTransformBase.cs b/Assets/Mirror/Components/NetworkTransformBase.cs index 1cfe66875..59c4fbda5 100644 --- a/Assets/Mirror/Components/NetworkTransformBase.cs +++ b/Assets/Mirror/Components/NetworkTransformBase.cs @@ -42,11 +42,21 @@ public abstract class NetworkTransformBase : NetworkBehaviour public readonly SortedList serverSnapshots = new SortedList(); // selective sync ////////////////////////////////////////////////////// - [Header("Selective Sync & Interpolation\nDon't change these at Runtime")] + [Header("Selective Sync\nDon't change these at Runtime")] public bool syncPosition = true; // do not change at runtime! public bool syncRotation = true; // do not change at runtime! public bool syncScale = false; // do not change at runtime! rare. off by default. + // interpolation is on by default, but can be disabled to jump to + // the destination immediately. some projects need this. + [Header("Interpolation")] + [Tooltip("Set to false to have a snap-like effect on position movement.")] + public bool interpolatePosition = true; + [Tooltip("Set to false to have a snap-like effect on rotations.")] + public bool interpolateRotation = true; + [Tooltip("Set to false to remove scale smoothing. Example use-case: Instant flipping of sprites that use -X and +X for direction.")] + public bool interpolateScale = true; + // debugging /////////////////////////////////////////////////////////// [Header("Debug")] public bool showGizmos; @@ -141,7 +151,7 @@ protected void AddSnapshot(SortedList snapshots, doub // // NOTE: stuck detection is unnecessary here. // we always set transform.position anyway, we can't get stuck. - protected virtual void Apply(TransformSnapshot interpolated) + protected virtual void Apply(TransformSnapshot interpolated, TransformSnapshot endGoal) { // local position/rotation for VR support // @@ -150,9 +160,15 @@ protected virtual void Apply(TransformSnapshot interpolated) // -> we still interpolated // -> but simply don't apply it. if the user doesn't want to sync // scale, then we should not touch scale etc. - if (syncPosition) target.localPosition = interpolated.position; - if (syncRotation) target.localRotation = interpolated.rotation; - if (syncScale) target.localScale = interpolated.scale; + + if (syncPosition) + target.localPosition = interpolatePosition ? interpolated.position : endGoal.position; + + if (syncRotation) + target.localRotation = interpolateRotation ? interpolated.rotation : endGoal.rotation; + + if (syncScale) + target.localScale = interpolateScale ? interpolated.scale : endGoal.scale; } // client->server teleport to force position without interpolation. diff --git a/Assets/Mirror/Components/NetworkTransformReliable/NetworkTransformReliable.cs b/Assets/Mirror/Components/NetworkTransformReliable/NetworkTransformReliable.cs index 718335b4c..c1bc7708f 100644 --- a/Assets/Mirror/Components/NetworkTransformReliable/NetworkTransformReliable.cs +++ b/Assets/Mirror/Components/NetworkTransformReliable/NetworkTransformReliable.cs @@ -82,7 +82,7 @@ void UpdateServer() // interpolate & apply TransformSnapshot computed = TransformSnapshot.Interpolate(from, to, t); - Apply(computed); + Apply(computed, to); } } @@ -129,7 +129,7 @@ void UpdateClient() // interpolate & apply TransformSnapshot computed = TransformSnapshot.Interpolate(from, to, t); - Apply(computed); + Apply(computed, to); } diff --git a/Assets/Mirror/Components/NetworkTransformUnreliable/NetworkTransform.cs b/Assets/Mirror/Components/NetworkTransformUnreliable/NetworkTransform.cs index 35a9154f3..a1ac8800d 100644 --- a/Assets/Mirror/Components/NetworkTransformUnreliable/NetworkTransform.cs +++ b/Assets/Mirror/Components/NetworkTransformUnreliable/NetworkTransform.cs @@ -144,7 +144,7 @@ void UpdateServer() // interpolate & apply TransformSnapshot computed = TransformSnapshot.Interpolate(from, to, t); - Apply(computed); + Apply(computed, to); } } } @@ -235,7 +235,7 @@ void UpdateClient() // interpolate & apply TransformSnapshot computed = TransformSnapshot.Interpolate(from, to, t); - Apply(computed); + Apply(computed, to); } } } diff --git a/Assets/Mirror/Tests/Editor/NetworkTransform2kTests.cs b/Assets/Mirror/Tests/Editor/NetworkTransform2kTests.cs index 06d4e4192..338ea2670 100644 --- a/Assets/Mirror/Tests/Editor/NetworkTransform2kTests.cs +++ b/Assets/Mirror/Tests/Editor/NetworkTransform2kTests.cs @@ -11,7 +11,7 @@ public class NetworkTransformExposed : NetworkTransform { public new TransformSnapshot Construct() => base.Construct(); public new void Apply(TransformSnapshot interpolated) => - base.Apply(interpolated); + base.Apply(interpolated, interpolated); public new void OnClientToServerSync(Vector3? position, Quaternion? rotation, Vector3? scale) => base.OnClientToServerSync(position, rotation, scale); public new void OnServerToClientSync(Vector3? position, Quaternion? rotation, Vector3? scale) =>