아이템 위를 지나가면 아이템을 줍게 변경해보자
엔진 콜리전 추가
UCLASS()
class TESTUNREALENGINE_API AMyWeapon : public AActor
{
// ...
// 박스로 Trigger영역을 지정할 예정
UPROPERTY(VisibleAnywhere)
class UBoxComponent* Trigger;
};
AMyWeapon::AMyWeapon()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = false;
Weapon = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("WEAPON"));
Trigger = CreateDefaultSubobject<UBoxComponent>(TEXT("TRIGGER"));
static ConstructorHelpers::FObjectFinder<UStaticMesh> SW(TEXT("StaticMesh'/Game/ParagonGreystone/FX/Meshes/Heroes/Greystone/SM_Greystone_Blade_01.SM_Greystone_Blade_01'"));
if (SW.Succeeded())
{
Weapon->SetStaticMesh(SW.Object);
}
Weapon->SetupAttachment(RootComponent);
Trigger->SetupAttachment(Weapon);
Weapon->SetCollisionProfileName(TEXT("MyCollectible"));
Trigger->SetCollisionProfileName(TEXT("MyCollectible"));
Trigger->SetBoxExtent(FVector(30.f, 30.f, 30.f));
}
// ...
void AMyWeapon::PostInitializeComponents()
{
Super::PostInitializeComponents();
// 델리게이트 지정
Trigger->OnComponentBeginOverlap.AddDynamic(this, &AMyWeapon::OnCharacterOverlap);
}
void AMyWeapon::OnCharacterOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
UE_LOG(LogTemp, Log, TEXT("Overlapped"));
AMyCharacter* MyCharacter = Cast<AMyCharacter>(OtherActor);
if (MyCharacter)
{
FName WeaponSocket(TEXT("hand_l_socket"));
AttachToComponent(MyCharacter->GetMesh(),
FAttachmentTransformRules::SnapToTargetNotIncludingScale,
WeaponSocket);
}
}