C++AI的随机移动

这里面有一个小坑,有一些教程或者资料没有提及:

如果需要使用导航网格相关的内容,则需要在Build.cs中启用相关功能(NavigationSystem):

PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "GameplayTasks", "NavigationSystem" });

之后就可以在代码中对导航网格进行操作了:

UNavigationSystemV1* NavSystem = FNavigationSystem::GetCurrent<UNavigationSystemV1>(GetWorld());
FNavLocation Result;
bool bSuccess = NavSystem->GetRandomReachablePointInRadius(ControlledPawn->GetActorLocation(), 1000, Result);
//Out
const FVector OutResult = Result.Location;

OwnerController->MoveToLocation(OutResult,-1,false,true,true,true);

可以在任意时刻,比如Tick中,查询当前AI的行为状态,通过调用OwnerController->GetMoveStatus()来查询,会返回一个枚举,通过实际情况去判断:

namespace EPathFollowingStatus
{
    enum Type
    {
        /** No requests */
        Idle,

        /** Request with incomplete path, will start after UpdateMove() */
        Waiting,

        /** Request paused, will continue after ResumeMove() */
        Paused,

        /** Following path */
        Moving,
    };
}