Unexpectedly brake when AI agents use RequestDirectMove to move

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:

  1. UCharacterMovementComponent::ApplyRequestedMove normally consume this speed
  2. UCharacterMovementComponent::ShouldComputeAccelerationToReachRequestedVelocity will return true because 500 < 497 * 1.01
  3. UCharacterMovementComponent::CalcVelocity (CharacterMovementComponent.cpp : 3837) will invoke IsExceedingMaxSpeed, which will return true because 500^2 > 497 ^ 2 * 1.01
  4. 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);
}

重现步骤

  1. add a AI Character to level, with charactermovementcomponent MaxWalkSpeed=500
  2. set level blueprint the same as LevelBlueprint.png. In brief, we just request direct move at constant max speed every tick except a little bit smaller speed at one frame.
  3. start game
  4. check out the log, you will find a wired brake happens.

Hey Zhou Yang,

Thanks for the bug report and steps to reproduce the issue! I’ve reproduced the issue locally and made a Jira to track it. In the meantime, your solution with changing logic in ShouldComputeAccelerationToReachRequested does fix the issue you mentioned so it is definitely a decent workaround but that could have other unintended effects. My initial thought is to just avoid braking entirely for this specific scenario (RequestedSpeed < CurrentSpeed and within 1%) in UCharacterMovementComponent::CalcVelocity, but I’ll have to do some thorough testing to make sure there aren’t any adverse effects of this.

Thanks,

Nate

Thanks for your rely. Please let me know the CL if you fix it.