모든 아이템은 상호 작용 시 ActivateItem 함수를 실행하도록 설계하였다.
BaseItem 클래스의 ActivateItem에 Particle과 Sound를 스폰하는 로직을 추가한다면, 자식 아이템 클래스들도 상호작용 시 Particle과 Sound이 된다.
📌 아이템 클래스에 파티클/사운드 스폰 구현
✅ BaseItem.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "ItemInterface.h"
#include "BaseItem.generated.h"
UCLASS()
class STRIKEZONE_API ABaseItem : public AActor, public IItemInterface
{
GENERATED_BODY()
public:
ABaseItem();
protected:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Item|Effects")
UParticleSystem* PickupParticle;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Item|Effects")
USoundBase* PickupSound;
virtual void ActivateItem(AActor* Activator) override;
};
✅ BaseItem.cpp
void ABaseItem::ActivateItem(AActor* Activator)
{
UParticleSystemComponent* Particle = nullptr;
if (PickupParticle)
{
Particle = UGameplayStatics::SpawnEmitterAtLocation(
GetWorld(),
PickupParticle,
GetActorLocation(),
GetActorRotation(),
true
);
}
if (Particle)
{
FTimerHandle DestroyParticleTimerHandle;
GetWorld()->GetTimerManager().SetTimer(
DestroyParticleTimerHandle,
[Particle]()
{
Particle->DestroyComponent();
},
2.0f,
false
);
}
if (PickupSound)
{
UGameplayStatics::PlaySoundAtLocation(
GetWorld(),
PickupSound,
GetActorLocation()
);
}
}
- UGameplayStatics::SpawnEmitterAtLocation() 함수로 Particle 스폰
- SetTimer로 2초뒤에 USceneComponent::DestoryComponent() 를 호출하여 Component 파괴
- UGameplayStatics::PlaySoundAtLocation() 함수로 Sound 재생
✅ Mine.cpp
AMineItem::AMineItem()
{
bHasExploded = false;
}
void AMineItem::ActivateItem(AActor* Activator)
{
if (bHasExploded) return;
Super::ActivateItem(Activator);
GetWorld()->GetTimerManager().SetTimer
(
ExplosionTimerHandle,
this,
&AMineItem::Explode,
ExplosionDelay,
false
);
bHasExploded = true;
}
- MineItem의 경우 상호작용되면 사라지는 것이 아니라, 몇 초 월드에 머물러 있다가 폭발하며 사라지게 되는데
이런 경우, 월드에 머물러 있는 시간동안 ActiveItem이 반복적으로 호출될 수 있는 문제가 있다. - bHasExpoled 변수를 선언해 ActivateItem이 최초 한 번만 상호작용 가능하도록 설정
- Super::Activate(Activator); 로 부모의 Activate 함수를 호출하여 정상적으로 파티클과 사운드 재생이 될 수 있게 한다.
🔔 결과
'내배캠 > Unreal Engine' 카테고리의 다른 글
네트워크 개념 (0) | 2025.03.10 |
---|---|
동적 멀티캐스트 델리게이트 (Dynamic Multicast Delegate) (0) | 2025.02.11 |
Widget Component로 월드에 UI 배치하기 (0) | 2025.02.07 |
UI 애니메이션 효과 만들기 (0) | 2025.02.07 |
게임 메뉴 UI 디자인 / 게임 흐름에 맞게 UI 전환 (0) | 2025.02.06 |