AI의 공격패턴 중 Dash Attack을 개발하고자 한다.
Dash에는 다양한 애니메이션이 있고, 자체적으로 Root Motion이 있는 애니메이션도 있겠지만 현재 개발하고자 하는 것은 날아서 Dash를 하는 애니메이션이며 Root Motion은 없기에 직접 Actor를 움직여야 한다.
UFUNCTION(BlueprintImplementableEvent, Category = "CPP Function")
void Dashing(FVector Destination);
BlueprintImplementableEvent를 사용하여 블루프린트에서 해당 함수를 정의하고자 하였다.
사실 복잡한 함수를 만드려는 것은 아니기에 C++로 작성할 수도 있으나 현재 프로젝트는 블루프린트와 C++의 범용성에 초점을 두고 있기에 이와 같이 개발하였다.
void ATitan::DashAttack()
{
TitanAnim->DashAttack();
float DeltaTime = GetWorld()->GetDeltaSeconds();
float TimeElapsed = 0.0f;
ACharacter* Player = UGameplayStatics::GetPlayerCharacter(GetWorld(), 0);
FVector Destination = Player->GetActorLocation();
GetWorldTimerManager().SetTimer(DashTimerHandle, [this, DeltaTime, TimeElapsed, Destination, Player]() mutable {
TimeElapsed += DeltaTime;
float Alpha = TimeElapsed / 1.13f;
if (Alpha >= 0.4f) {
Dashing(Destination);
if (Alpha <= 0.6f) {
Destination = Player->GetActorLocation();
}
}
if (Alpha >= 1.13f) {
GetWorldTimerManager().ClearTimer(DashTimerHandle);
}
}, DeltaTime, true);
}
Enemy내 DashAttack 함수이다.
AnimInstance에 있는 DashAttack 함수는 단순하게 Montage를 Play하는 역할만 수행한다.
이후에는 Timer를 조금 응용해서 블루프린트의 Timeline과 같이 작동하도록 하였다.
1.13초 동안 Timer를 설정한 것은 애니메이션의 PlayTime이고, 0.4초 이후에 Dashing 함수를 실행시킨 이유는 애니메이션의 동작이 0.4초 이후 Dash동작을 이어나가기 때문이다.
0.4초에서 0.6초까지 0.2초간은 Player를 추격하도록 하고, 이후에는 0.6초 때 저장된 Location값을 토대로 Enemy를 이동시켰다.
이후 PlayTime을 초과하면 ClearTimer를 통해 Timer를 종료시킴으로써 공격을 마치도록 하였다.
