[언리얼] C++ - AIToMove

gest·2026년 1월 7일

unreal

목록 보기
5/11

라이브러리

Build.cs에 AIModule, NavigationSystem를 추가한다

PublicDependencyModuleNames.AddRange(new string[] { 
    "Core", 
    "CoreUObject", 
    "Engine", 
    "InputCore",
    "AIModule", //AI
    "NavigationSystem"  // 경로 찾기 기능
});
//cpp
#include "AIController.h"
#include "NavigationSystem.h"  // 내비게이션 사용 시

코드

AAIController* AIController = Cast<AAIController>(GetController());
ACharacter* playerPawn = UGameplayStatics::GetPlayerCharacter(GetWorld(), 0);

if (AIController)
{
	AIController->SetFocus(playerPawn);
	AIController->MoveToActor(playerPawn, 100); //추격할_대상, 탐지_반지름
}

AIToMove OnSuccess


만약 AIMoveTo 함수를 OnSuccess 한 함수를 넣고 싶다면 어떻게 해야할까?
여기서는 콜백함수를 넣어서 해결할 수 있다.
바로 OnRequestFinished 함수다.

//.h
// 전방 선언 추가. const FPathFollowingResult&를 해결하기 위한 전반 선언
struct FAIRequestID;
struct FPathFollowingResult;



//.cpp
#include "Navigation/PathFollowingComponent.h" 
//GetPathFollowingComponent에 있는 OnRequestFinished
#include "AITypes.h"


//콜백 설정
aiController->GetPathFollowingComponent()->OnRequestFinished.AddUObject(this, &AEnemy::MoveCompleted);


//추격을 완료했다면 => 콜백함수
void AEnemy::MoveCompleted(FAIRequestID RequestID, const FPathFollowingResult& Result)
{
	UE_LOG(LogTemp, Log, TEXT("dd"));

	//USkeletalMeshComponent* mesh = GetMesh();
	if (PPAPCanAttack)
	{
		PlayAnimMontage(AttackMontage); //지속시간 매개변수 있음
	}
}


0개의 댓글