In our project using UE5.6.1, AI characters sometimes brake unexpectedly. After one hour debugging, the cause of problem has been found.
If we invoke RequestDirectMove with a speed that is a little bit smaller than current speed, for example MaxSpeed=500 RequestSpeed=497, the code will run in the following way:
- UCharacterMovementComponent::ApplyRequestedMove normally consume this speed
- UCharacterMovementComponent::ShouldComputeAccelerationToReachRequestedVelocity will return true because 500 < 497 * 1.01
- UCharacterMovementComponent::CalcVelocity (CharacterMovementComponent.cpp : 3837) will invoke IsExceedingMaxSpeed, which will return true because 500^2 > 497 ^ 2 * 1.01
- UCharacterMovementComponent::CalcVelocity (CharacterMovementComponent.cpp : 3845) will invoke ApplyVelocityBraking, which will add a significant speed reduction.
This bug is really a coincidence. I think a reasonable way to fix it is change the check logic in ShouldComputeAccelerationToReachRequested :
bool UCharacterMovementComponent::ShouldComputeAccelerationToReachRequestedVelocity(const float RequestedSpeed) const
{
return bRequestedMoveUseAcceleration && Velocity.SizeSquared() < FMath::Square(RequestedSpeed * 0.99f);
}