diff --git a/Assets/Mirror/Core/SnapshotInterpolation/SnapshotInterpolation.cs b/Assets/Mirror/Core/SnapshotInterpolation/SnapshotInterpolation.cs index d36de8b2d..36387608d 100644 --- a/Assets/Mirror/Core/SnapshotInterpolation/SnapshotInterpolation.cs +++ b/Assets/Mirror/Core/SnapshotInterpolation/SnapshotInterpolation.cs @@ -130,7 +130,11 @@ public static double TimelineClamp( // outside of the area, we clamp. double lowerBound = targetTime - bufferTime; double upperBound = targetTime + bufferTime; +#if !UNITY_2021_OR_NEWER + return Extensions.Clamp(localTimeline, lowerBound, upperBound); +#else return Math.Clamp(localTimeline, lowerBound, upperBound); +#endif } // call this for every received snapshot. diff --git a/Assets/Mirror/Core/Tools/Extensions.cs b/Assets/Mirror/Core/Tools/Extensions.cs index 02407e09f..ca394e8bb 100644 --- a/Assets/Mirror/Core/Tools/Extensions.cs +++ b/Assets/Mirror/Core/Tools/Extensions.cs @@ -59,6 +59,14 @@ public static bool TryDequeue(this Queue source, out T element) element = default; return false; } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T Clamp(this T val, T min, T max) where T : IComparable + { + if (val.CompareTo(min) < 0) return min; + else if (val.CompareTo(max) > 0) return max; + else return val; + } #endif } }