diff --git a/Assets/BetterPhysics/Runtime/Scripts/BetterRigidbody.cs b/Assets/BetterPhysics/Runtime/Scripts/BetterRigidbody.cs index 46fd663..17e21c6 100644 --- a/Assets/BetterPhysics/Runtime/Scripts/BetterRigidbody.cs +++ b/Assets/BetterPhysics/Runtime/Scripts/BetterRigidbody.cs @@ -57,11 +57,11 @@ public void SetLimit(int index, SpeedLimit limit) { public int LimitCount => limits.Count; private void FixedUpdate() { - if (!_rb.isKinematic) { + if (!_rb.isKinematic && _exemptedVelocityChange != Vector3.zero) { // Apply exempted newtons directly to the velocity _rb.AddLinearVelocity(_exemptedVelocityChange); + _exemptedVelocityChange = Vector3.zero; } - _exemptedVelocityChange = Vector3.zero; ApplyLimits(); } @@ -176,7 +176,8 @@ private void ApplyHardLimit(SpeedLimit limit) { switch (limit.Directionality) { case Directionality.Omnidirectional: Vector3 clampedVelocity = Vector3.ClampMagnitude(expectedNewVelocity, limit.ScalarLimit); - + + if (currentVelocity == clampedVelocity && accumulatedNewtons == 0) return; // We're hard clamping so remove all accumulated force _rb.AddForce(-accumulatedNewtons); _rb.SetLinearVelocity(clampedVelocity); @@ -193,6 +194,8 @@ private void ApplyHardLimit(SpeedLimit limit) { clampedNewVelocity[i] = Mathf.Clamp(clampedNewVelocity[i], -max[i], max[i]); } } + + if (currentVelocity == clampedVelocity && accumulatedNewtons == 0) return; // We're hard clamping so remove all accumulated force _rb.AddForce(-accumulatedNewtons); @@ -216,6 +219,8 @@ private void ApplyHardLimit(SpeedLimit limit) { Vector3 clampedWorldVelocity = _rb.rotation * clampedLocalVelocity; + if (currentVelocity == clampedWorldVelocity && accumulatedNewtons == 0) return; + // We're hard clamping so remove all accumulated force _rb.AddForce(-accumulatedNewtons); _rb.SetLinearVelocity(clampedWorldVelocity); @@ -838,4 +843,4 @@ private Vector3 CalculateVelocityChangeWithSoftLimit(in Vector3 currentVelocity, #endregion } -} \ No newline at end of file +}