From 074e0d6dc65a69dae6adf0967727ae1738e05e2d Mon Sep 17 00:00:00 2001 From: Robin Rolf Date: Fri, 15 Mar 2024 14:12:20 +0000 Subject: [PATCH] chore: old unity compat for PredictedRigidbody (#3781) --- .../PredictedRigidbody/PredictedRigidbody.cs | 52 +++++++++---------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/Assets/Mirror/Components/PredictedRigidbody/PredictedRigidbody.cs b/Assets/Mirror/Components/PredictedRigidbody/PredictedRigidbody.cs index b5d4ebadd..21e029c91 100644 --- a/Assets/Mirror/Components/PredictedRigidbody/PredictedRigidbody.cs +++ b/Assets/Mirror/Components/PredictedRigidbody/PredictedRigidbody.cs @@ -348,32 +348,6 @@ protected virtual void SmoothFollowPhysicsCopy() // float positionStep = (distance * distance) * interpolationSpeed; float positionStep = distance * positionInterpolationSpeed; - // simple and slow version with MoveTowards, which recalculates delta and delta.sqrMagnitude: - // Vector3 newPosition = Vector3.MoveTowards(currentPosition, physicsPosition, positionStep * deltaTime); - // faster version copied from MoveTowards: - // this increases Prediction Benchmark Client's FPS from 615 -> 640. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - static Vector3 MoveTowardsCustom( - Vector3 current, - Vector3 target, - Vector3 _delta, // pass this in since we already calculated it - float _sqrDistance, // pass this in since we already calculated it - float _distance, // pass this in since we already calculated it - float maxDistanceDelta) - { - if (_sqrDistance == 0.0 || maxDistanceDelta >= 0.0 && _sqrDistance <= maxDistanceDelta * maxDistanceDelta) - return target; - - float distFactor = maxDistanceDelta / _distance; // unlike Vector3.MoveTowards, we only calculate this once - return new Vector3( - // current.x + (_delta.x / _distance) * maxDistanceDelta, - // current.y + (_delta.y / _distance) * maxDistanceDelta, - // current.z + (_delta.z / _distance) * maxDistanceDelta); - current.x + _delta.x * distFactor, - current.y + _delta.y * distFactor, - current.z + _delta.z * distFactor); - } - Vector3 newPosition = MoveTowardsCustom(currentPosition, physicsPosition, delta, sqrDistance, distance, positionStep * deltaTime); // smoothly interpolate to the target rotation. @@ -385,6 +359,32 @@ static Vector3 MoveTowardsCustom( tf.SetPositionAndRotation(newPosition, newRotation); } + // simple and slow version with MoveTowards, which recalculates delta and delta.sqrMagnitude: + // Vector3 newPosition = Vector3.MoveTowards(currentPosition, physicsPosition, positionStep * deltaTime); + // faster version copied from MoveTowards: + // this increases Prediction Benchmark Client's FPS from 615 -> 640. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + static Vector3 MoveTowardsCustom( + Vector3 current, + Vector3 target, + Vector3 _delta, // pass this in since we already calculated it + float _sqrDistance, // pass this in since we already calculated it + float _distance, // pass this in since we already calculated it + float maxDistanceDelta) + { + if (_sqrDistance == 0.0 || maxDistanceDelta >= 0.0 && _sqrDistance <= maxDistanceDelta * maxDistanceDelta) + return target; + + float distFactor = maxDistanceDelta / _distance; // unlike Vector3.MoveTowards, we only calculate this once + return new Vector3( + // current.x + (_delta.x / _distance) * maxDistanceDelta, + // current.y + (_delta.y / _distance) * maxDistanceDelta, + // current.z + (_delta.z / _distance) * maxDistanceDelta); + current.x + _delta.x * distFactor, + current.y + _delta.y * distFactor, + current.z + _delta.z * distFactor); + } + // destroy visual copy only in OnStopClient(). // OnDestroy() wouldn't be called for scene objects that are only disabled instead of destroyed. public override void OnStopClient()