From 0b00351822906b635a19b9c91dd41bcc5e8beb42 Mon Sep 17 00:00:00 2001 From: mischa Date: Wed, 6 Sep 2023 19:33:50 +0200 Subject: [PATCH] MovePosition/Rotation --- .../PredictedRigidbody/PredictedRigidbody.cs | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/Assets/Mirror/Components/PredictedRigidbody/PredictedRigidbody.cs b/Assets/Mirror/Components/PredictedRigidbody/PredictedRigidbody.cs index 740239e44..51fcddc20 100644 --- a/Assets/Mirror/Components/PredictedRigidbody/PredictedRigidbody.cs +++ b/Assets/Mirror/Components/PredictedRigidbody/PredictedRigidbody.cs @@ -13,11 +13,32 @@ public class PredictedRigidbody : NetworkBehaviour [Tooltip("Broadcast changes if position changed by more than ... meters.")] public float positionSensitivity = 0.01f; + [Header("Smoothing")] + public bool smoothCorrection = true; + void Awake() { rb = GetComponent(); } + void ApplyState(Vector3 position, Quaternion rotation, Vector3 velocity) + { + // Rigidbody .position teleports, while .MovePosition interpolates + // TODO is this a good idea? what about next capture while it's interpolating? + if (smoothCorrection) + { + rb.MovePosition(position); + rb.MoveRotation(rotation); + } + else + { + rb.position = position; + rb.rotation = rotation; + } + + rb.velocity = velocity; + } + void UpdateServer() { if (Vector3.Distance(transform.position, lastPosition) >= positionSensitivity) @@ -51,10 +72,7 @@ public override void OnDeserialize(NetworkReader reader, bool initialState) Vector3 velocity = reader.ReadVector3(); // hard force for now. - // TODO compare past position at timestamp, and only correct if needed - rb.position = position; - rb.rotation = rotation; - rb.velocity = velocity; + ApplyState(position, rotation, velocity); } } }