如果要使用 UE4自带的自动寻路功能,除了需要调用寻路相关的API以外,还要设置寻路组件:NavMeshBoundsVolume,否则调用寻路API时无效。
更详细的说明见官方文档:https://docs.unrealengine.com/latest/INT/Resources/ContentExamples/NavMesh/index.html
1,首先向场景中拖入NavMeshBoundsVolume组件:
2,然后点击build,build成功以后,按键盘P键,可以看到build后的有效寻路区域,绿色标识
3,这个时候就可以使用相关的寻路API了。下面是官方TopDown模版中使用的自动寻路代码:
UNavigationSystem* const NavSys = GetWorld()->GetNavigationSystem();
void AMyPlayerController::SetNewMoveDestination(const FVector DestLocation) { APawn* const Pawn = GetPawn(); if (Pawn) { UNavigationSystem* const NavSys = GetWorld()->GetNavigationSystem(); float const Distance = FVector::Dist(DestLocation, Pawn->GetActorLocation()); // We need to issue move command only if far enough in order for walk animation to play correctly if (NavSys && (Distance > 120.0f)) { NavSys->SimpleMoveToLocation(this, DestLocation); } } }