// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "AIController.h"
#include "EnemyCharacter.h"
#include "EnemyAIController.generated.h"
/**
* 在界面-体积-NavMeshBoundsVolume(导航网格) ,拖到场景中设置,按下P键显示
在ProjectSetting的NavigationMesh-Generation-CellSize和Cellheight 设置值
*/
UCLASS()
class PACMAN_API AEnemyAIController : public AAIController
{
GENERATED_BODY()
public:
//要重写的函数
//相当于beginplay,每当开启一个ai的时候,首先执行Possess
void Possess(APawn* InPawn)override;
//移动停止后调用
virtual void OnMoveCompleted(FAIRequestID RequestID,const FPathFollowingResult& Result)override;
void SearchNewPoint();
void StopMove();
private:
AEnemyCharacter * Bot;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "EnemyAIController.h"
#include "AI/Navigation//NavigationSystem.h"
void AEnemyAIController::Possess(APawn * InPawn)
{
Super::Possess(InPawn);
Bot = Cast<AEnemyCharacter>(InPawn);
SearchNewPoint();
}
void AEnemyAIController::OnMoveCompleted(FAIRequestID RequestID, const FPathFollowingResult & Result)
{
SearchNewPoint();
}
void AEnemyAIController::SearchNewPoint()
{
//通过导航网格来实现
//获取导航系统
UNavigationSystem* NavMesh = UNavigationSystem::GetCurrent(this);
if (NavMesh){
//搜索半径
const float SearchRadius = 1000.0f;
FNavLocation RandomPt;
//返回是否能找到
const bool bFound=NavMesh->GetRandomReachablePointInRadius(Bot->GetActorLocation(), SearchRadius, RandomPt);
if (bFound)
{
//移动到找到的点
MoveToLocation(RandomPt);
}
}
}
void AEnemyAIController::StopMove()
{
//停止移动
StopMovement();
}