이번 코드는 단순히 주위에 미니언이 존재하는지 확인 후, 거리에 따라 플레이어를 같이 추적하는 코드를 작성한다. AI 캐릭터가 메시지를 보내는 작업은 매우 간단하다. 게임 모드가 서버에 있는 한, 게임 내의 모든 액터에서 접근할 수 있기 때문이다.
US_Minion.cpp
void AUS_Minion::Chase(APawn* Pawn)
{
...
if (const auto GameMode = Cast<AUS_GameMode>(GetWorld()->GetAuthGameMode()))
{
GameMode->AlertMinions(this, Pawn->GetActorLocation(), AlertRadius);
}
}
GameMode에서 AlertMinions()를 실행하여 미니언에게 플레이어 위치를 알려주는 함수를 호출한다.
US_GameMode.cpp
void AUS_GameMode::AlertMinions(AActor* AlertInstigator, const FVector& Location, const float Radius)
{
TArray<AActor*> Minions;
UGameplayStatics::GetAllActorsOfClass(GetWorld(), AUS_Minion::StaticClass(), Minions);
for (const auto Minion : Minions)
{
if (AlertInstigator == Minion)
continue;
if (const auto Distance = FVector::Distance(AlertInstigator->GetActorLocation(), Minion->GetActorLocation()); Distance < Radius)
{
if (const auto MinionCharacter = Cast<AUS_Minion>(Minion))
{
MinionCharacter->GoToLocation(Location);
}
}
}
}
- AlertInstigator: 이 매개변수를 통해 알람을 울린 미니언을 확인할 수 있다.
- UGameplayStatics::GetAllActorsOfClass: 월드에 존재하는 모든 미니언들을 가져온다.
코드를 보면 결국 알람을 호출한 미니언을 제외한 모든 미니언의 거리를 확인하고, 일정 거리 안에 존재한다면 위치를 통해 플레이어를 추적한다.
'Unreal > 언리얼 엔진 5로 개발하는 멀티플레이어 게임(Book)' 카테고리의 다른 글
[Unreal] US_BaseWeaponProjectile.cpp 코드 분석 (0) | 2025.04.02 |
---|---|
[Unreal] US_Minion.cpp (체력) (0) | 2025.04.02 |
[Unreal] 소음, 감지 코드 분석 (0) | 2025.04.01 |
[Unreal] US_MinionSpawner.cpp 코드 분석 (0) | 2025.03.30 |
[Unreal] US_Minion.cpp 코드 분석(AI) (0) | 2025.03.30 |