Unreal5 Camera 와 Spring Arm C++

250x250

Unreal5 Camera와 Spring arm C++

안녕하세요.

저번에 캐릭터 이동은 구현했는데 카메라가 이상했었습니다.

그래서 이번에는 카메라를 추가하고, spring arm을 달아서 3인칭으로 볼 수 있게 만들겠습니다.

[프로그래밍/언리얼] - Unreal5 Enhanced Input 캐릭터 이동 및 점프 C++

 

Unreal5 Enhanced Input 캐릭터 이동 및 점프 C++

Unreal5 Enhanced input 캐릭터 이동 및 점프 C++ 안녕하세요. 오늘은 언리얼5에서 EnHanced input을 사용해서 캐릭터 이동과 점프를 구현해 보겠습니다. 그냥 공부한 내용 대충 작성하는거라 효율이랑은 거

intunknown.tistory.com

  • SlayerCharacter.h 코드 수정.
  • SlayerCharacter.cpp 코드 수정.
  • 결과.

SlayerCharacter.h

먼저 Camera와 Spring Arm class를 추가해 주었습니다.

/*camera, spring arm*/
class USpringArmComponent;
class UCameraComponent;

private에도 추가합니다.

private:
	/*camera,spring arm*/
	USpringArmComponent* CameraBoom;
	UCameraComponent* ViewCamera;

현재 SlayerCharacter.h 전체 코드.

더보기
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "InputActionValue.h"
#include "SlayerCharacter.generated.h"
/*input*/
class UInputMappingContext;
class UInputAction;
/*camera, spring arm*/
class USpringArmComponent;
class UCameraComponent;
UCLASS()
class SLAYER_API ASlayerCharacter : public ACharacter
{
	GENERATED_BODY()
public:
	// Sets default values for this character's properties
	ASlayerCharacter();
	// Called every frame
	virtual void Tick(float DeltaTime) override;
	// Called to bind functionality to input
	virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	/*Enhanced input*/ 
	//#include "InputActionValue.h"
	UPROPERTY(EditAnywhere, Category=Input)
	UInputMappingContext* SlayerContext;
	UPROPERTY(EditAnywhere, Category = Input)
	UInputAction* MovementAction;
	UPROPERTY(EditAnywhere, Category = Input)
	UInputAction* JumpAction;
	//UPROPERTY(EditAnywhere, Category = Input)
	//UInputAction* LookAction;
	//void Look(const FInputActionValue& value);
	void Move(const FInputActionValue& value);
	void Jump() override;
private:
	/*camera,spring arm*/
	USpringArmComponent* CameraBoom;
	UCameraComponent* ViewCamera;
};

SlayerCharacter.cpp 코드 수정.

생성자에서 Spring Arm과 Camera를 생성하는 코드를 작성하고, 생성한 오브젝트를 추가하였습니다.

/*spring arm*/
//#include "GameFramework/SpringArmComponent.h"필요
CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
CameraBoom->SetupAttachment(GetRootComponent());
CameraBoom->TargetArmLength = 300.f;
/*Camera*/
ViewCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("ViewCamera"));
ViewCamera->SetupAttachment(CameraBoom);

https://docs.unrealengine.com/4.27/en-US/API/Runtime/Engine/Camera/UCameraComponent/

 

UCameraComponent

Represents a camera viewpoint and settings, such as projection type, field of view, and post-process overrides.

docs.unrealengine.com

https://docs.unrealengine.com/5.1/en-US/API/Runtime/Engine/GameFramework/USpringArmComponent/

 

USpringArmComponent

This component tries to maintain its children at a fixed distance from the parent, but will retract the children if there is a collision, and spring back when there is no collision.

docs.unrealengine.com

SlayerCharacter.cpp 전체 코드.

더보기
// Fill out your copyright notice in the Description page of Project Settings.
#include "Character/SlayerCharacter.h"
/*input*/
#include "EnhancedInputSubsystems.h"
#include "EnhancedInputComponent.h"
/*camera, spring arm*/
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
// Sets default values
ASlayerCharacter::ASlayerCharacter()
{
 	// Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
	/*spring arm*/
	//#include "GameFramework/SpringArmComponent.h"필요
	CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
	CameraBoom->SetupAttachment(GetRootComponent());
	CameraBoom->TargetArmLength = 300.f;
	/*Camera*/
	ViewCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("ViewCamera"));
	ViewCamera->SetupAttachment(CameraBoom);
}

// Called when the game starts or when spawned
void ASlayerCharacter::BeginPlay()
{
	Super::BeginPlay();
	//#include "EnhancedInputSubsystems.h" 추가 필요.
	if (APlayerController* PlayerController = Cast<APlayerController>(GetController())) {
		if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer())) {
			Subsystem->AddMappingContext(SlayerContext, 0);
		}
	}
}

void ASlayerCharacter::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 ASlayerCharacter::Jump()
{
	Super::Jump(); //Character.h에 있는 Jump사용.
}

// Called every frame
void ASlayerCharacter::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

// Called to bind functionality to input
void ASlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);
	//#include "EnhancedInputComponent.h"추가.
	if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent)) {
		EnhancedInputComponent->BindAction(MovementAction, ETriggerEvent::Triggered, this, &ASlayerCharacter::Move);
		EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Triggered, this, &ASlayerCharacter::Jump);
	}
}

결과.

Camera와 Spring arm을 추가하니 뒤에 카메라가 하나 따라오는 것을 확인할 수 있네요.

이 작업을 진행하기 전 실행을 시켜보면 움직이는 것은 가능하지만 몸속에서 무언가를 보는 느낌이었습니다.

(아래 사진 참고.)

Spring Arm과 Camera를 추가한 이후 카메라가 멀리 떨어져 있어 3인칭으로 보여주게 됩니다.

상황에 따라 카메라 거리가 늘었다 줄었다 하는 기능도 생겼네요.

Designed by JB FACTORY