오늘은 이제 캐릭터를 만들어 볼 것이다

 

일단 캐릭터 C++클래스부터 만들어보자 

 

그리고 이를 활용한 블루프린트클래스도 만들어주자 만들게 되면 기본적으로 캡슐컴포넌트와 매쉬등이 만들어져있다.

이때 매쉬에서 캐릭터매쉬를 하나 지정해주고 애니메이션도 Idle로 하나 넣어주자

 

그리고 이제 움직일 수 있게 입력액션과 입력매핑컨텍스트를 넣어주자 

입력액션에서는 좌우로도 움직일 수 있게 Vector2D로 입력을 받도록하자.

입력액션

입력컨텍스트에서는 앞뒤 좌우로 움직일 수 있게 wasd로 매핑해줄 것인데 이때 ad는 x축으로 이동하는것으로 그대로 두면 되지만 ws는 y축으로 움직이는 것으로 Modifiers에 스위질 입력축 값과 negate를 추가해주어야한다. 

 

이제 이에 맞는 코드를 작성해주자 

이전에 했던 것과 동일하게 매핑컨텍스트와 액션매핑 및 콜백함수를 헤더파일에서 정의하고 

BeginPlay에서 입력컨텍스트를 추가해준다음 그 입력컨텍스트에 함수들을 붙여주고 

함수를 정의해주면 된다.

그리고 FInputActionValue를 헤더에서 사용할 때는 헤더파일을 generated.h 전에 include 시켜주어야한다.

 

Slash.h

	#include "InputActionValue.h"
    
	class UInputMappingContext;
	class UInputAction;
private:
	UPROPERTY(EditAnywhere,Category = Input)
	UInputMappingContext* SlashContext;

	UPROPERTY(EditAnywhere,Category = Input)
	UInputAction* MovementAction;
    
    void Move(const FInputActionValue& Value);

 

Slash.cpp

void ASlashCharacter::BeginPlay()
{
	Super::BeginPlay();
	
	//조건문안에 넣는 것이 최적화상으로 좋다
	if (APlayerController* PlayerController = Cast<APlayerController>(GetController()))
	{
		if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem< UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
		{
			Subsystem->AddMappingContext(SlashContext, 0);
		}
	}

}

void ASlashCharacter::Move(const FInputActionValue& Value)
{
	const FVector2D MovementVector = Value.Get<FVector2D>();

	//보는곳으로 이동할 수 있게
	const FRotator Rotation = Controller->GetControlRotation();
	const FRotator YawRotation(0.f, Rotation.Yaw, 0.f);
	
	//보는 방향가져오기
	const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
	AddMovementInput(ForwardDirection, MovementVector.Y);
	const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
	AddMovementInput(RightDirection, MovementVector.X);
}

void ASlashCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent))
	{
		EnhancedInputComponent->BindAction(MovementAction, ETriggerEvent::Triggered, this, &ASlashCharacter::Move);
		//EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &ASlashCharacter::Look);
	}

}

 

 

핫리로딩해준 다음 블루프린트에서 해당 입력 칸을 채워주면 된다.

결과

이제 카메라추가를 해보자

전방선언을 통해 스프링암과 카메라를 추가해주자

Slash.h

class USpringArmComponent;
class UCameraComponent;

UPROPERTY(VisibleAnywhere)
USpringArmComponent* SpringArm;

UPROPERTY(VisibleAnywhere)
UCameraComponent* ViewCamera;

 

Slash.cpp

#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"

ASlashCharacter::ASlashCharacter()
{
 
	PrimaryActorTick.bCanEverTick = true;

	SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArm"));
	SpringArm->SetupAttachment(GetRootComponent());
	SpringArm->TargetArmLength = 300.f;

	ViewCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("ViewCamera"));
	ViewCamera->SetupAttachment(SpringArm);
}

 

핫리로딩을 해주면 카메라와 스프링암이 생긴것을 볼 수 있다.

 

이제 캐릭터에 머리와 눈썹을 달아주자 

이때 눈썹과 머리 에셋이 로드가 안될 수도 있는 데 이때는 groom에 해당하는 2개의 플러그인은 체크해주고 재시작하면 

로드가 잘된다.


코드상으로 머리와 눈썹 변수를 생성해주자 

일단 모듈을 가져올 수 있도록 빌드.cs 코드에 "HairStrandsCore", "Niagara" 두개를 추가해준다. 그리고 

Binaries Intermediate Saved 이 3개의 파일을 지운 뒤 uproject파일을 generate해준다. 

그리고 다시 들어가서 빌드해주면 모듈을 가져올 수 있다.

 

이제 헤더파일에 전방선언을 통해 UGroomComponent 포인터 객체를 2개 선언해주자

class UGroomComponent;
    
UPROPERTY(VisibleAnywhere,Category=Hair)
UGroomComponent* Hair;

UPROPERTY(VisibleAnywhere,Category = Hair)
UGroomComponent* Eyebrows;

 

그리고 생성자에 변수를 초기화해주고 소켓부분을 만들어주자

	//소켓지정
	Hair->AttachmentName = FString("head");
	
	Eyebrows = CreateDefaultSubobject<UGroomComponent>(TEXT("Eyebrows"));
	Eyebrows->SetupAttachment(GetMesh());
	//소캣지정
	Eyebrows->AttachmentName = FString("head");

 

그리고 컴파일해주면 Eyebrows와 Hair가 메시 하위에 생긴 것을 볼 수 있다. 그리고 그룸컴포넌트는 GroomAsset을 넣을 수 있는 곳이 있는데 여기에 눈썹과 머리를 넣어주면 된다.

 

이렇게하면 머리와 눈썹을 달 수 있다!

+ Recent posts