// Fill out your copyright notice in the Description page of Project Settings.
#include "BTService_Detect.h"
#include "HamsterDemoCharacter.h"
#include "MonsterAIController.h"
#include "BehaviorTree/BlackboardComponent.h"
#include "DrawDebugHelpers.h"
UBTService_Detect::UBTService_Detect()
{
NodeName = TEXT("Detect");
Interval = 1.0f;
}
void UBTService_Detect::TickNode(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory, float DeltaSeconds)
{
Super::TickNode(OwnerComp, NodeMemory, DeltaSeconds);
APawn* ControllingPawn = OwnerComp.GetAIOwner()->GetPawn();
if (ControllingPawn == nullptr)
{
UE_LOG(LogTemp, Log, TEXT("Detect Failed - ControllingPawn nullptr"));
return;
}
UWorld* World = ControllingPawn->GetWorld();
FVector Center = ControllingPawn->GetActorLocation();
float DetectRadius = 800.0f;
if (World == nullptr)
return;
TArray<FOverlapResult> OverlapResults;
FCollisionQueryParams CollisionQueryParam(NAME_None, false, ControllingPawn);
bool bResult = World->OverlapMultiByChannel(
OverlapResults,
Center,
FQuat::Identity,
ECollisionChannel::ECC_GameTraceChannel3,
FCollisionShape::MakeSphere(DetectRadius),
CollisionQueryParam
);
if (bResult)
{
for (auto OverlapResult : OverlapResults)
{
AHamsterDemoCharacter* HamsterDemoCharacter = Cast<AHamsterDemoCharacter>(OverlapResult.GetActor());
if (HamsterDemoCharacter && HamsterDemoCharacter->GetController()->IsPlayerController())
{
OwnerComp.GetBlackboardComponent()->SetValueAsObject(AMonsterAIController::TargetKey, HamsterDemoCharacter);
DrawDebugSphere(World, Center, DetectRadius, 16, FColor::Green, false, 0.2f);
DrawDebugPoint(World, HamsterDemoCharacter->GetActorLocation(), 10.0f, FColor::Blue, false, 0.2f);
DrawDebugLine(World, ControllingPawn->GetActorLocation(), HamsterDemoCharacter->GetActorLocation(), FColor::Blue, false, 0.2f);
return;
}
}
}
else
{
OwnerComp.GetBlackboardComponent()->SetValueAsObject(AMonsterAIController::TargetKey, nullptr);
}
DrawDebugSphere(World, Center, DetectRadius, 16, FColor::Red, false, 0.2f);
}
플레이어가 일정 범위 내에 있을 때 이를 감지하여 플레이어를 따라오는, BTService 를 기반으로 한 Detect
프로젝트 폴더의 config ->DefaultEngine.ini 에서 오브젝트에 배정된 트레이스 채널을 확인할 수 있다.