Unreal5 Camera 와 Spring Arm C++
- 프로그래밍/언리얼
- 2023. 4. 30.
250x250
Unreal5 Camera와 Spring arm C++
안녕하세요.
저번에 캐릭터 이동은 구현했는데 카메라가 이상했었습니다.
그래서 이번에는 카메라를 추가하고, spring arm을 달아서 3인칭으로 볼 수 있게 만들겠습니다.
[프로그래밍/언리얼] - Unreal5 Enhanced Input 캐릭터 이동 및 점프 C++
- 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/
https://docs.unrealengine.com/5.1/en-US/API/Runtime/Engine/GameFramework/USpringArmComponent/
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인칭으로 보여주게 됩니다.
상황에 따라 카메라 거리가 늘었다 줄었다 하는 기능도 생겼네요.