• 무기는 스켈레톤 메쉬로 되어 있음으로 코드추가하고
  • 스켈레톤 메쉬에 경로를 찾아서 적용해준다
  • 마지막에 소켓을 부모로 해서 연결해준다.
//.h
    UPROPERTY(VisibleAnyWhere, Category = Weapon)
        USkeletalMeshComponent* Weapon;

//.cpp

    FName WeaponSocket(TEXT("hand_rSocket"));
    if (GetMesh()->DoesSocketExist(WeaponSocket))
    {
        Weapon = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("WEAPON"));
        static ConstructorHelpers::FObjectFinder<USkeletalMesh> SK_WEAPON(TEXT(
            "/Game/InfinityBladeWeapons/Weapons/Blade/Swords/Blade_BlackKnight/SK_Blade_BlackKnight.SK_Blade_BlackKnight"));

        if (SK_WEAPON.Succeeded())
        {
            Weapon->SetSkeletalMesh(SK_WEAPON.Object);
        }

        Weapon->SetupAttachment(GetMesh(), WeaponSocket);
    }

무기 액터 클래스 제작
위 코드와 흡사하게 제작

/.h
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once
#include "KGame.h"
#include "GameFramework/Actor.h"
#include "ABWeapon.generated.h"

UCLASS()
class KGAME_API AABWeapon : public AActor
{
    GENERATED_BODY()

public: 
    // Sets default values for this actor's properties
    AABWeapon();

protected:
    // Called when the game starts or when spawned
    virtual void BeginPlay() override;

public: 
    // Called every frame
    virtual void Tick(float DeltaTime) override;
public:
    UPROPERTY(VisibleAnyWhere, Category = Weapon)
        USkeletalMeshComponent* Weapon;
};


//cpp
AABWeapon::AABWeapon()
{
    // 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<USkeletalMeshComponent>(TEXT("WEAPON"));
    RootComponent = Weapon;

    static ConstructorHelpers::FObjectFinder<USkeletalMesh> SK_WEAPON(TEXT(
        "/Game/InfinityBladeWeapons/Weapons/Blade/Swords/Blade_BlackKnight/SK_Blade_BlackKnight.SK_Blade_BlackKnight"));

    if (SK_WEAPON.Succeeded())
    {
        Weapon->SetSkeletalMesh(SK_WEAPON.Object);
    }

    Weapon->SetCollisionProfileName(TEXT("NoCOllision"));  //충돌 프리셋 설정
}

 

기존 캐릭터 클래스에 무기 헤더 추가해서 적용
기존 캐릭터에 있는 무기 관련 코드는 제거
SpawnActor<>() = 액터를 생성하는 명령

void AABCharacter::BeginPlay()
{
    Super::BeginPlay();
    FName WeaponSocket(TEXT("hand_rSocket"));
    auto CurWeapon = GetWorld()->SpawnActor<AABWeapon>(FVector::ZeroVector, FRotator::ZeroRotator);

    if (nullptr != CurWeapon)
    {
        CurWeapon->AttachToComponent(GetMesh(), FAttachmentTransformRules::SnapToTargetNotIncludingScale, WeaponSocket);
    }
}

+ Recent posts