From e7c889867944e9af06e99870840b474d7fbf8da0 Mon Sep 17 00:00:00 2001 From: mischa <16416509+vis2k@users.noreply.github.com> Date: Wed, 14 Feb 2024 10:41:14 +0100 Subject: [PATCH] Prediction: only set velocity if not kinematic --- .../PredictedRigidbody/PredictedRigidbody.cs | 18 +++++++++++++----- .../PredictedRigidbody/PredictionUtils.cs | 9 +++++++-- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/Assets/Mirror/Components/PredictedRigidbody/PredictedRigidbody.cs b/Assets/Mirror/Components/PredictedRigidbody/PredictedRigidbody.cs index 3983da8f0..805ef907a 100644 --- a/Assets/Mirror/Components/PredictedRigidbody/PredictedRigidbody.cs +++ b/Assets/Mirror/Components/PredictedRigidbody/PredictedRigidbody.cs @@ -495,8 +495,12 @@ void ApplyState(double timestamp, Vector3 position, Quaternion rotation, Vector3 // to stop and start moving again on client - slide as well here. predictedRigidbody.position = position; predictedRigidbody.rotation = rotation; - predictedRigidbody.velocity = velocity; - predictedRigidbody.angularVelocity = angularVelocity; + // projects may keep Rigidbodies as kinematic sometimes. in that case, setting velocity would log an error + if (!predictedRigidbody.isKinematic) + { + predictedRigidbody.velocity = velocity; + predictedRigidbody.angularVelocity = angularVelocity; + } // clear history and insert the exact state we just applied. // this makes future corrections more accurate. @@ -536,9 +540,13 @@ void ApplyState(double timestamp, Vector3 position, Quaternion rotation, Vector3 predictedRigidbody.rotation = rotation; } - // there's only one way to set velocity - predictedRigidbody.velocity = velocity; - predictedRigidbody.angularVelocity = angularVelocity; + // there's only one way to set velocity. + // (projects may keep Rigidbodies as kinematic sometimes. in that case, setting velocity would log an error) + if (!predictedRigidbody.isKinematic) + { + predictedRigidbody.velocity = velocity; + predictedRigidbody.angularVelocity = angularVelocity; + } } // process a received server state. diff --git a/Assets/Mirror/Components/PredictedRigidbody/PredictionUtils.cs b/Assets/Mirror/Components/PredictedRigidbody/PredictionUtils.cs index f1da27c6b..335ce0bf2 100644 --- a/Assets/Mirror/Components/PredictedRigidbody/PredictionUtils.cs +++ b/Assets/Mirror/Components/PredictedRigidbody/PredictionUtils.cs @@ -23,7 +23,6 @@ public static void MoveRigidbody(GameObject source, GameObject destination) rigidbodyCopy.mass = original.mass; rigidbodyCopy.drag = original.drag; rigidbodyCopy.angularDrag = original.angularDrag; - rigidbodyCopy.angularVelocity = original.angularVelocity; rigidbodyCopy.useGravity = original.useGravity; rigidbodyCopy.isKinematic = original.isKinematic; rigidbodyCopy.interpolation = original.interpolation; @@ -33,7 +32,13 @@ public static void MoveRigidbody(GameObject source, GameObject destination) rigidbodyCopy.freezeRotation = original.freezeRotation; rigidbodyCopy.position = original.position; rigidbodyCopy.rotation = original.rotation; - rigidbodyCopy.velocity = original.velocity; + + // projects may keep Rigidbodies as kinematic sometimes. in that case, setting velocity would log an error + if (!original.isKinematic) + { + rigidbodyCopy.velocity = original.velocity; + rigidbodyCopy.angularVelocity = original.angularVelocity; + } // destroy original GameObject.Destroy(original);