무기변경을 해보자
만약 왼손에 뭔가를 쥐게하고 싶다면 메쉬 -> 스켈레톤 트리 -> hand_l에 소켓을 추가하자
AMyCharacter::AMyCharacter()
{
// ...
// 소켓을 찾고
FName WeaponSocket(TEXT("hand_l_socket"));
// 소켓이 있을경우
if (GetMesh()->DoesSocketExist(WeaponSocket))
{
// Static Mesh에 붙여준다
Weapon = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("WEAPON"));
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(GetMesh(), WeaponSocket);
}
}
무기를 바닥에 두었다가 줍게 만들어보자
C++클래스로 MyWeapon 생성
UCLASS()
class TESTUNREALENGINE_API AMyWeapon : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyWeapon();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
UPROPERTY(VisibleAnywhere)
UStaticMeshComponent* Weapon;
};
AMyWeapon::AMyWeapon()
{
// 틱을 돌리지 않기에 false로 둔다
PrimaryActorTick.bCanEverTick = false;
// 나머지는 위와 동일
Weapon = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("WEAPON"));
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->SetCollisionProfileName(TEXT("NoCollision"));
}
void AMyCharacter::BeginPlay()
{
Super::BeginPlay();
FName WeaponSocket(TEXT("hand_l_socket"));
// 무기 스폰
auto CurrentWeapon = GetWorld()->SpawnActor<AMyWeapon>(FVector::ZeroVector, FRotator::ZeroRotator);
// 무기 줍기
if (CurrentWeapon)
{
CurrentWeapon->AttachToComponent(GetMesh(),
FAttachmentTransformRules::SnapToTargetNotIncludingScale,
WeaponSocket);
}
}