// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "AIController.h"
#include "Enemy.h"
#include "AIEnemy.generated.h"
/**
*
*/
UCLASS()
class PACMAN_API AAIEnemy : public AAIController
{
GENERATED_BODY()
public:
// 要重写的函数
// 相当于beginplay,每当开启一个AI的时候调用,首先执行OnPossess
void OnPossess(class APawn* InPawn) override;
// 移动停止后调用
virtual void OnMoveCompleted(FAIRequestID RequestID, const FPathFollowingResult& Result) override;
void SearchNewPoint();
private:
class AEnemy* Bot;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "Public/AIEnemy.h"
#include "NavigationSystem.h"
void AAIEnemy::OnPossess(class APawn* InPawn)
{
Super::OnPossess(InPawn);
// 获得Enemy,肉体
Bot = Cast<AEnemy>(InPawn);
}
void AAIEnemy::OnMoveCompleted(FAIRequestID RequestID, const FPathFollowingResult & Result)
{
}
void AAIEnemy::SearchNewPoint()
{
// 判断导航网格在不在
UNavigationSystemV1* NavMesh = UNavigationSystemV1::GetCurrent(this);
// 如果在
if (NavMesh)
{
// 设置搜索半径
const float SearchRadius = 1000.0f;
// 设置一个点
FNavLocation RandomPt;
// 以敌人的位置为中心,这个半径为范围,找一个点,
const bool bFound = NavMesh->GetRandomReachablePointInRadius(Bot->GetActorLocation(), SearchRadius, RandomPt);
// 如果能找到
if (bFound)
{
// 移动到这个点
MoveToLocation(RandomPt);
}
}
}
注意:
Possess()使用OnPossess代替
使用UNavigationSystemV1*,需要引入头文件:#include "NavigationSystem.h"