이제 항아리는 잘 부숴지는데 이것을 하나의 기본 Actor로 만들어주자. 그러려면 Actor에 Geometry Collection이 있어야한다.
BreakableActor.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "BreakableActor.generated.h"
class UGeometryCollectionComponent;
UCLASS()
class SLASH_API ABreakableActor : public AActor
{
GENERATED_BODY()
public:
ABreakableActor();
virtual void Tick(float DeltaTime) override;
protected:
virtual void BeginPlay() override;
private:
UPROPERTY(VisibleAnywhere)
UGeometryCollectionComponent* GeometryCollection;
};
BreakableActor.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "Breakable/BreakableActor.h"
#include "GeometryCollection/GeometryCollectionComponent.h"
ABreakableActor::ABreakableActor()
{
PrimaryActorTick.bCanEverTick = false;
GeometryCollection = CreateDefaultSubobject<UGeometryCollectionComponent>(TEXT("GeometryCollection"));
SetRootComponent(GeometryCollection);
GeometryCollection->SetGenerateOverlapEvents(true);
}
void ABreakableActor::BeginPlay()
{
Super::BeginPlay();
}
void ABreakableActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
이렇게 해주고 Build.cs 코드에 모듈을 추가해줘야 정상적으로 빌드된다.
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore","EnhancedInput","HairStrandsCore", "Niagara", "GeometryCollectionEngine" });
이렇게 해주고 이를 바탕으로한 BlueprintClass를 만들어주자.
이렇게 해주면 Geometry Collection이 생성된 블루프린트가 나오는데 이때 카오스 피직스의 컬렉션지정으로 기본 켈렉션을 지정해줄 수 있다.
이렇게 해주고 씬에 배치해보면 잘 작동하는 것을 볼 수 있다.
이제 부숴질 때의 동작을 IHitInterface를 상속받아서 작동하도록 만들어주자.
이때 override한 GetHit이 블루프린트상에서도 호출가능하게 하고 싶을 수 있는데 이때 BluePrint Natvie Event를 사용하면 된다.
HitInterface.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "UObject/Interface.h"
#include "HitInterface.generated.h"
// This class does not need to be modified.
UINTERFACE(MinimalAPI)
class UHitInterface : public UInterface
{
GENERATED_BODY()
};
/**
*
*/
class SLASH_API IHitInterface
{
GENERATED_BODY()
// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:
UFUNCTION(BlueprintNativeEvent)
void GetHit(const FVector& ImpactPoint);
};
이렇게 해주고 기존의 GetHit의 함수의 이름 을 GetHit_Implementation로 다 바꿔주자.
그리고 실제로 호출될 때는 언리얼엔진 에서 제공해주는 Execute함수를 통해 실행되도록 하자.
Weapon.cpp
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();
TArray<AActor*> ActorsToIgnore;
ActorsToIgnore.Add(this);
for (AActor* Actor : IgnoreActors)
{
ActorsToIgnore.AddUnique(Actor);
}
//충돌 결과
FHitResult BoxHit;
UKismetSystemLibrary::BoxTraceSingle(
this,
Start,
End,
FVector(5.f, 5.f, 5.f),
BoxTraceStart->GetComponentRotation(),
ETraceTypeQuery::TraceTypeQuery1,
false,
ActorsToIgnore, //무시할거
EDrawDebugTrace::ForDuration,
BoxHit,
true //자신무시
);
if (BoxHit.GetActor())
{
IHitInterface* HitInterface = Cast<IHitInterface>(BoxHit.GetActor());
if (HitInterface)
{
HitInterface->Execute_GetHit(BoxHit.GetActor(), BoxHit.ImpactPoint);
}
IgnoreActors.AddUnique(BoxHit.GetActor());
CreateFields(BoxHit.ImpactPoint);
}
}
이렇게 해주면 블루프린트상에서도 GetHit을 사용할 수 있다. 이때 부모의 Get Hit이 코드상의 GetHit이고 이벤트는 블루프린트 상의 새로운 GetHit이다.
이제 이 함수를 통해 부숴질 때 소리가 들리도록 구현해보자. 오디오소스를 임포트해주고 메타사운드를 만들어주자.
이렇게 해주고 Play Sound at location을 추가해주면 소리가 잘 재생되는 것을 볼 수 있다.
여기서 set Life Span을 설정해주면 부숴지고 나서 그 오브젝트가 Destroy되게 설정해줄 수 있다. 이때 notify Break를 켜주어야한다.
이렇게 해주면 연쇄적으로 다른 것이 부숴지더라도 같이 없어지는 것을 볼 수 있다.
'게임공부 > Unreal Engine' 카테고리의 다른 글
[Unreal Engine][C++]18. Treasure2 (0) | 2025.01.03 |
---|---|
[Unreal Engine][C++]17. Treasure (0) | 2025.01.03 |
[Unreal Engine][C++]15. Breakable Actors (0) | 2024.12.31 |
[Unreal Engine][C++]14. Hit React (3) | 2024.12.27 |
[Unreal Engine][C++]13. Enemy Class (0) | 2024.09.25 |