fix(PredictedRigidbody): now snaps into place below a velocity threshold to fix dancing rigidbodies near rest position due to prediction fighting with corrections

This commit is contained in:
mischa 2024-01-08 11:33:31 +01:00
parent e32914969f
commit ba16e2f949

View File

@ -85,6 +85,9 @@ public class PredictedRigidbody : NetworkBehaviour
[Tooltip("Configure how to apply the corrected state.")]
public CorrectionMode correctionMode = CorrectionMode.Move;
[Tooltip("Server & Client would sometimes fight over the final position at rest. Instead, hard snap into black below a certain velocity threshold.")]
public float snapThreshold = 2;
[Header("Visual Interpolation")]
[Tooltip("After creating the visual interpolation object, keep showing the original Rigidbody with a ghost (transparent) material for debugging.")]
public bool showGhost = true;
@ -332,6 +335,19 @@ void ApplyCorrection(RigidbodyState corrected, RigidbodyState before, RigidbodyS
// TODO don't hardcode length?
Debug.DrawLine(corrected.position, corrected.position + corrected.velocity * 0.1f, Color.white, lineTime);
// fix rigidbodies seemingly dancing in place instead of coming to rest.
// hard snap to the position below a threshold velocity.
// this is fine because the visual object still smoothly interpolates to it.
if (rb.velocity.magnitude <= snapThreshold)
{
Debug.Log($"Prediction: snapped {name} into place because velocity {rb.velocity.magnitude:F3} <= {snapThreshold:F3}");
stateHistory.Clear();
rb.position = corrected.position;
rb.rotation = corrected.rotation;
rb.velocity = Vector3.zero;
return;
}
// now go through the history:
// 1. skip all states before the inserted / corrected entry
// 3. apply all deltas after timestamp