Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions Assets/BetterPhysics/Runtime/Scripts/BetterRigidbody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Comment on lines +63 to 64
Copy link

Copilot AI Apr 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current condition may prevent _exemptedVelocityChange from being reset when _rb.isKinematic is true, potentially leaving stale data for future frames. Consider resetting _exemptedVelocityChange outside the conditional block to ensure it is always cleared.

Suggested change
_exemptedVelocityChange = Vector3.zero;
}
}
_exemptedVelocityChange = Vector3.zero;

Copilot uses AI. Check for mistakes.
_exemptedVelocityChange = Vector3.zero;

ApplyLimits();
}
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -838,4 +843,4 @@ private Vector3 CalculateVelocityChangeWithSoftLimit(in Vector3 currentVelocity,

#endregion
}
}
}