언리얼

unreal) 특정 거리 안에 들어오면 회전하는 타워

HJH0825 2024. 11. 28. 20:54

Tower.cpp

void ATower::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	if(Tank)
	{
		float Distance = FVector::Dist(GetActorLocation(), Tank->GetActorLocation());
		if (Distance <= FireRange)
		{
			RotateTurret(Tank->GetActorLocation());
		}
	}
}

void ATower::BeginPlay()
{
	Super::BeginPlay();

	Tank = Cast<ATank>(UGameplayStatics::GetPlayerPawn(this, 0));
}

 

 

BeginPlay에서 현재 PlayerPawn을 가져온다. (Tank)  매번 호출하기에는 부하가 있으니

Tank = Cast<ATank>(UGameplayStatics::GetPlayerPawn(this, 0));

 

그 이후 Tick()에서 특정 거리 안에 들어오면 Rotate처리

float Distance = FVector::Dist(GetActorLocation(), Tank->GetActorLocation());

FVector::Dist를 통해 Tank와 거리 계산