From 8c4e9bbcef023ec9843a769a776a460305dcaeed Mon Sep 17 00:00:00 2001 From: mischa Date: Thu, 28 Mar 2024 21:23:01 +0800 Subject: [PATCH] add callbacks again --- .../PredictedRigidbody/PredictedRigidbody.cs | 39 ++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/Assets/Mirror/Components/PredictedRigidbody/PredictedRigidbody.cs b/Assets/Mirror/Components/PredictedRigidbody/PredictedRigidbody.cs index bec3ec6e8..0fd5e6a69 100644 --- a/Assets/Mirror/Components/PredictedRigidbody/PredictedRigidbody.cs +++ b/Assets/Mirror/Components/PredictedRigidbody/PredictedRigidbody.cs @@ -426,6 +426,7 @@ protected virtual bool IsMoving() => predictedRigidbody.velocity.sqrMagnitude >= motionSmoothingVelocityThresholdSqr || predictedRigidbody.angularVelocity.sqrMagnitude >= motionSmoothingAngularVelocityThresholdSqr; + // TODO maybe merge the IsMoving() checks & callbacks with UpdateState(). void UpdateGhosting() { // perf: enough to check ghosts every few frames. @@ -469,10 +470,46 @@ void UpdateGhosting() } } + // when using Fast mode, we don't create any ghosts. + // but we still want to check IsMoving() in order to support the same + // user callbacks. + bool lastMoving = false; + void UpdateState() + { + // perf: enough to check ghosts every few frames. + // PredictionBenchmark: only checking every 4th frame: 770 => 800 FPS + if (Time.frameCount % checkGhostsEveryNthFrame != 0) return; + + bool moving = IsMoving(); + + // started moving? + if (moving && !lastMoving) + { + OnBeginPrediction(); + lastMoving = true; + } + // stopped moving? + else if (!moving && lastMoving) + { + // ensure a minimum time since starting to move, to avoid on/off/on effects. + if (NetworkTime.time >= motionSmoothingLastMovedTime + motionSmoothingTimeTolerance) + { + OnEndPrediction(); + lastMoving = false; + } + } + } + void Update() { if (isServer) UpdateServer(); - if (isClientOnly && mode == PredictionMode.Smooth) UpdateGhosting(); + if (isClientOnly) + { + if (mode == PredictionMode.Smooth) + UpdateGhosting(); + else if (mode == PredictionMode.Fast) + UpdateState(); + } } void LateUpdate()