BehaviorTree 단순 좌표 이동

2025. 5. 22. 14:56·Unreal 팁

ㅇBehaviorTree에서 AI의 단순한 좌표이동을 만들어보겠습니다

특정 위치로 이동하면서 Offset을 주어 해당 좌표의 랜덤위치를 살짝 더해서 좌표를 구해보겠습니다

오프셋이 더해진 특정 Vector로 이동을 하고 그 위치로 이동을 하면 다른행동을 하도록 해보겠습니다

좌측에서 위치로 이동한뒤

우측으로 신호를 보내보겠습니다

 

사용할 Bool Key 그리고 Vector Key를 사용하고있는 BlackBoard에서 만들어주겠습니다

 

BT_Task를 만들어줍니다

UCLASS()
class BLASTERDREAM_API UTask_BossAISetPosition : public UBTTask_BlackboardBase
{
	GENERATED_BODY()
public:
	UTask_BossAISetPosition();
	virtual EBTNodeResult::Type ExecuteTask(UBehaviorTreeComponent& ownerComp, uint8* nodeMemory) override;

	UPROPERTY(EditAnywhere, Category="MySettings")
	FVector positionVector;

	UPROPERTY(EditAnywhere, Category = "MySettings")
	float offSetRange = 100.0f;

	UPROPERTY(EditAnywhere, Category = "MySettings")
	FBlackboardKeySelector positionVectorKey;
};

 

사용할헤더 추가해줍니다

#include "Task_BossAISetPosition.h"
#include "AIController.h"
#include "Math/UnrealMathUtility.h"
#include "BehaviorTree/BlackboardComponent.h"
#include "Kismet/KismetMathLibrary.h"

 

UTask_BossAISetPosition::UTask_BossAISetPosition()
{
	NodeName = "PositionSet";
}

EBTNodeResult::Type UTask_BossAISetPosition::ExecuteTask(UBehaviorTreeComponent& ownerComp, uint8* nodeMemory)
{
	UBlackboardComponent* blackboardComp = ownerComp.GetBlackboardComponent();
	AAIController* aiCon = ownerComp.GetAIOwner();
	APawn* aiPawn = aiCon->GetPawn();
	FVector randomOffset = FVector(FMath::FRandRange(-offSetRange, offSetRange),FMath::FRandRange(-offSetRange, offSetRange),
		0.f 
	);
	FVector finalPosition = positionVector + randomOffset;
	blackboardComp->SetValueAsVector(positionVectorKey.SelectedKeyName, finalPosition);
	return EBTNodeResult::Succeeded;
}

UPROPERTY의 positionVector위치에 randomOffset값 랜덤을 구한 값으로 이동하는 Task코드입니다

그럼 그 위치로 이동을 다 하면 bAIPositionSet을 false에서 true로 만들어줄 Service를 만들어주겠습니다

 

Service_Task코드입니다

UCLASS()
class BLASTERDREAM_API UService_CheckAIPosition : public UBTService_BlackboardBase
{
	GENERATED_BODY()
public:
	UService_CheckAIPosition();
	virtual void TickNode(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory, float DeltaSeconds) override;

	UPROPERTY(EditAnywhere, Category = "MySettings")
	FBlackboardKeySelector positionKey;

	UPROPERTY(EditAnywhere, Category = "MySettings")
	FBlackboardKeySelector bAIPositionSetKey;
};

 

필요한 헤더입니다

#include "AIController.h"
#include "BehaviorTree/BlackboardComponent.h"

 

앞서 만들어놓은 목표지점 Vector키를 받아 그 벡터의 허용범위오차 200 안에 있으면 도착

lastPos2D와 currentPos2D는 Z축(높이)을 무시하고 XY 평면상의 위치만 표현한 벡터 입니다

FVector::DistSquared를 사용하여 두 벡터 사이의 제곱을 계산하여 거리를  계산합니다

허용범위 200안에 도착하면 VectorKey를 True로 설정합니다

UService_CheckAIPosition::UService_CheckAIPosition()
{
	NodeName = "PositionCheck";
	bNotifyTick = true;
}

void UService_CheckAIPosition::TickNode(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory, float DeltaSeconds)
{
	Super::TickNode(OwnerComp, NodeMemory, DeltaSeconds);
	
	AAIController* aICon = OwnerComp.GetAIOwner();
	APawn* aiPawn = aICon->GetPawn();
	UBlackboardComponent* blackBoard = aICon->GetBlackboardComponent();
	FVector lastPositionVector = blackBoard->GetValueAsVector(positionKey.SelectedKeyName);
	FVector currentAIPosition = aiPawn->GetActorLocation();

	FVector lastPos2D(lastPositionVector.X, lastPositionVector.Y, 0.f);
	FVector currentPos2D(currentAIPosition.X, currentAIPosition.Y, 0.f);

	//오차범위
	const float allowableArrivalRange = 200.f;
	const float allowableArrivalRangeSq = allowableArrivalRange * allowableArrivalRange;

	if (FVector::DistSquared(currentPos2D, lastPos2D) < allowableArrivalRangeSq)
	{
		blackBoard->SetValueAsBool(bAIPositionSetKey.SelectedKeyName, true);
	}
}

 

BehaviorTree의 Selector에 Service를 추가하고 양쪽에 Decorator를 달아 조건을 달아줍니다

왼쪽은 bAIPositionSet의 bool Key의 is Not Set

우측은 bAIPositionSet의 bool Key의 is Set

 

 

각각 사용할 Key를 할당해줍니다

 

AI는 Move To로 지정해놓은 Vector로 이동한다음

이동이 완료되면 bAIPositionSet Key가 true로 바뀌어 우측의 노드가 실행됩니다

'Unreal 팁' 카테고리의 다른 글

언리얼 - 폰트 변경하기  (0) 2025.06.09
FBX파일이 아닌 에셋 언리얼에셋으로 컨버트시키기  (0) 2025.05.27
Unreal - 유용한 무료 에셋 사이트 (에셋구하는법)  (0) 2025.04.23
언리얼 GitHub(깃허브) 플러그인 공유하는법  (0) 2025.04.13
언리얼 GitHub(깃허브) 프로젝트 공유하는법  (1) 2025.04.13
'Unreal 팁' 카테고리의 다른 글
  • 언리얼 - 폰트 변경하기
  • FBX파일이 아닌 에셋 언리얼에셋으로 컨버트시키기
  • Unreal - 유용한 무료 에셋 사이트 (에셋구하는법)
  • 언리얼 GitHub(깃허브) 플러그인 공유하는법
lucodev
lucodev
커피와 노트북 그리고 개발
  • lucodev
    루코 개발테이블
    lucodev
  • 전체
    오늘
    어제
    • 분류 전체보기 (121) N
      • Unreal5 프로젝트 다이어리 (73)
      • Unreal5 프로젝트 다이어리2 (3) N
      • Unreal 팁 (8)
      • Unreal 디버깅 (8)
      • C++ 프로그래머스 다이어리 (21) N
        • Stack (3)
        • Hash (4)
        • Heap (2)
        • Sort (1) N
      • 코드 개인보관함 (8) N
  • 인기 글

  • 최근 글

  • 최근 댓글

  • 링크

  • 공지사항

  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 태그

    언리얼 컷씬
    unreal 컷씬
    언리얼
    언리얼 로딩창
    언리얼 behaviortree
    unreal 모션매칭
    언리얼 foot step
    언리얼 behavior tree
    언리얼 비헤이비어트리
    unreal 시퀀스
    unreal 로딩
    unreal look at
    언리얼 motionmatching
    unreal sequence
    unreal loading
    언리얼 로딩
    언리얼 모션매칭
    언리얼 look at
    언리얼 시퀀스
    언리얼 페이드 아웃
  • hELLO· Designed By정상우.v4.10.3
lucodev
BehaviorTree 단순 좌표 이동
상단으로

티스토리툴바