본문 바로가기
[ CODING STUDY ]/》언리얼엔진5 실습

언리얼엔진5 SpawnAIFromClass

by MRG 2024. 5. 9.
728x90
반응형

 

▣ 먼저 Enemy cpp로가서 AIControllerClass에 대한 설정과 AutoPossessAI 설정을 하는 코드를 작성합니다.

 

▣  AutoPossessAI 종류 

Disabled 기능이 비활성화되었습니다(AI를 자동으로 보유하지 않음).
PlacedInWorld 폰이 세계에 배치된 경우에만 AI 컨트롤러가 소유합니다.
Spawned 월드가 로드된 후 Pawn이 스폰된 경우에만 AI 컨트롤러가 소유합니다.
PlacedInWorldOrSpawned 폰은 생성될 때마다 AI 컨트롤러에 의해 자동으로 소유됩니다.

 

 

▣ AI가 있는 스폰 하려면 Unreal Engine에서 "Spawn Actor for Class" 대신 "Spawn AI From Class" 노드를 사용해야 합니다.
이는 AI 캐릭터가 실제로 폰과 컨트롤러(또 다른 액터)라는 두 액터로 구성되어 있고, "클래스용 액터 생성"은 관련 컨트롤러가 아닌 "폰" 액터만 생성하기 때문입니다.

 

https://docs.unrealengine.com/4.26/en-US/API/Runtime/Engine/Engine/EAutoPossessAI/

 

EAutoPossessAI

Specifies if an AI pawn will automatically be possed by an AI controller

docs.unrealengine.com

 

 

▣ AIController cpp코드로 가서 BehaviorTree를 호출하는 코드전체를 주석처리합니다. 
만약 그렇게 하지 않으면 SpawnAIFromClass를 사용할 때 BehaviorTree에 대한 호출이 동시에 되어 오류가 발생하고 언리얼이 튕기는 현상이 있습니다. 

 

 

▣ 컴파일 후 Enemy블루프린트로 가서 Auto Possess AI와 AI Controller Class가 잘 적용되어 있는지 확인하고 

 

 

▣ Spawn AIFrom Class 노드를 사용하여 스폰하면 AIController이 적용되어 스폰됩니다.

 

 

 

 

▣ 이번에는  Spawn AIFrom Class를 c++로 사용하는 코드를 작성하겠습니다.
먼저 사용할 헤더 두 개를 추가하고 각각 적용할 pawn과 BehaviorTree변수를 선언합니다.
pawn과 BehaviorTree변수에 블루프린트로나 c++코드로 변수에 오브젝트랑 class를 값을 넣고
UAIBlueprintHelperLibrary에 접근하여  SpawnAIFromClass함수를 호출하여 코드를 작성합니다. 

 

https://dev.epicgames.com/documentation/en-us/unreal-engine/API/Runtime/AIModule/Blueprint/UAIBlueprintHelperLibrary/SpawnAIFromClass?application_version=5.0

 

Uai Blueprint Helper Library/Spawn Ai From Class | Unreal Engine 5.4 Documentation | Epic Developer Community

Spawns AI agent of a given class.

dev.epicgames.com

 

 

▣ 컴파일 저장 후 플레이 시 한 개가 스폰되고 AIController에 맞게 작동하는 걸 확인할 수 있습니다.

 

 

▣ for문을 사용하여 여러 명을 한꺼번에 스폰할 수도 있습니다. 

 

 

 

https://docs.unrealengine.com/4.26/en-US/BlueprintAPI/Math/Random/RandomPointinBoundingBox/

 

Random Point in Bounding Box

Random Point in Bounding Box

docs.unrealengine.com

 

▣ Target Point를 생성하여 Random Point in bounding Box 블루프린트로 해당 Box 주위로  Spawn 하게 할 수도 있습니다. 

 

 

 

▣ Target Point를 3개 생성하여 Array를 만들어 3개의 Target Point 중에 한 개를 랜덤으로 생성하게 만들 수도 있습니다. 

728x90
반응형

댓글