스토리지

공격 시 공격받은 객체 가져오기 본문

Unreal Engine/0. Unreal 5

공격 시 공격받은 객체 가져오기

ljw4104 2023. 7. 21. 18:46

1. 아이템에 컴포넌트 설치

  • Weapon에 Box Component와 Overlap 범위를 측정할 Scene Component 2개 (Start, End) 를 코드에서 추가한다.
UPROPERTY(VisibleAnywhere, Category = "Weapon Properties")
UBoxComponent* WeaponBox;

UPROPERTY(VisibleAnywhere)
USceneComponent* BoxTraceStart;

UPROPERTY(VisibleAnywhere)
USceneComponent* BoxTraceEnd;

2. 박스의 Overlap 이벤트 구현

void AWeapon::OnBoxOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp,
                           int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	// 월드 좌표계에서 각각의 컴포넌트의 좌표 벡터를 가져옴.
	const FVector Start = BoxTraceStart->GetComponentLocation();
	const FVector End = BoxTraceEnd->GetComponentLocation();

	// Overlap을 무시할 액터들을 지정
	TArray<AActor*> ActorsToIgnore;
	ActorsToIgnore.Add(this);

	for (AActor* Actor : IgnoreActors)
	{
		ActorsToIgnore.AddUnique(Actor);
	}

	FHitResult BoxHit; // Overlap 결과
	UKismetSystemLibrary::BoxTraceSingle(
		this, // UObect*
		Start,
		End,
		FVector(5.f, 5.f, 5.f), // Overlap을 측정할 상자크기
		BoxTraceStart->GetComponentRotation(),
		ETraceTypeQuery::TraceTypeQuery1,
		false,
		ActorsToIgnore, // Overlap을 무시할 Actor들 배열
		EDrawDebugTrace::ForDuration, // 일정시간동안 Box를 디버그함.
		BoxHit,	// Overlap의 결과를 담은 구조체
		true	// 본인이 Overlap 되는걸 무시하는지?
	);

	// 결과를 추출
	if (AActor* Actor = BoxHit.GetActor())
	{
		// 뽑아낸 Actor가 IHitInterface으로의 형변환이 가능하면
		if (IHitInterface* HitInterface = Cast<IHitInterface>(Actor))
		{
			// 맞는 함수 호출
			HitInterface->GetHit(BoxHit.ImpactPoint);
		}
		// 두 번 겹쳐지면 안되기 때문에 Ignore할 대상 액터에 추가
		IgnoreActors.AddUnique(Actor);
		// 해당 벡터는 Overlap이 종료되었을 때 초기화 시킨다.
	}
}

3. 결과

맞았을 때 해당 위치에 주변으로 녹색 상자모양이 나오고 맞은 기점은 빨간색 점으로 표시되었다.

 

'Unreal Engine > 0. Unreal 5' 카테고리의 다른 글

아이템 주워서 팔에 붙이기  (0) 2023.07.21
IK Rig  (0) 2023.07.18
충돌 판정  (0) 2023.07.17
애니메이션 적용  (0) 2023.07.14
언리얼 Inverse Kinematic  (0) 2023.07.14
Comments