스토리지

카메라 추가 본문

Unreal Engine/0. Unreal 5

카메라 추가

ljw4104 2023. 7. 12. 18:10

1. Camera & Spring Arm

  • Camera는 우리가 늘 아는 카메라
  • Spring Arm은 카메라가 벽에 막힐 때 늘어났다 줄어들었다 하는 스프링 역할을 수행함. Unity에서는 Cinemachine에서 설정해야 했던 것을 여기서는 Component 하나로 해결 가능함.

1-1. 계층구조

  • Spring Arm > Camera 순으로 이루어져야 정상적으로 작동한다.

 

2. 추가하기

Bird.h

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "Bird.generated.h"

class USpringArmComponent;
class UCameraComponent;

UCLASS()
class SLASH_API ABird : public APawn
{
	GENERATED_BODY()
	
private:
	UPROPERTY(VisibleAnywhere)
	USpringArmComponent *CameraBoom;

	UPROPERTY(VisibleAnywhere)
	UCameraComponent *ViewCamera;
};

Bird.cpp

ABird::ABird()
{
	CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArm"));
	CameraBoom->SetupAttachment(GetRootComponent());
	CameraBoom->TargetArmLength = 300.f;
	
	ViewCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("ViewCamera"));
	ViewCamera->SetupAttachment(CameraBoom);
}

 

3. 결과 (움직임 + 카메라)

 

4. Bird 클래스 전체 코드

1. Bird.h

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

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "Bird.generated.h"

class UCapsuleComponent;
class USkeletalMeshComponent;
class USpringArmComponent;
class UCameraComponent;

UCLASS()
class SLASH_API ABird : public APawn
{
	GENERATED_BODY()

public:
	ABird();
	virtual void Tick(float DeltaTime) override;

	// Called to bind functionality to input
	virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
	
protected:
	virtual void BeginPlay() override;
	void MoveForward(float Value);
	void Turn(float Value);
	void LookUp(float Value);
	
private:
	UPROPERTY(VisibleAnywhere)
	UCapsuleComponent *Capsule;

	UPROPERTY(VisibleAnywhere)
	USkeletalMeshComponent *BirdMesh;

	UPROPERTY(VisibleAnywhere)
	USpringArmComponent *CameraBoom;

	UPROPERTY(VisibleAnywhere)
	UCameraComponent *ViewCamera;
};

2. Bird.cpp

#include "Pawns/Bird.h"
#include "Slash/Consts.h"
#include "Components/CapsuleComponent.h"
#include "Components/SkeletalMeshComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"

ABird::ABird()
{
	PrimaryActorTick.bCanEverTick = true;
	
	Capsule = CreateDefaultSubobject<UCapsuleComponent>(TEXT("Capsule"));	// 캡슐 컴포넌트 생성
	Capsule->SetCapsuleHalfHeight(20.f);
	Capsule->SetCapsuleRadius(15.f);
	SetRootComponent(Capsule);

	BirdMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("BirdMesh"));
	BirdMesh->SetupAttachment(GetRootComponent());

	CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArm"));
	CameraBoom->SetupAttachment(GetRootComponent());
	CameraBoom->TargetArmLength = 300.f;
	
	ViewCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("ViewCamera"));
	ViewCamera->SetupAttachment(CameraBoom);
	
	AutoPossessPlayer = EAutoReceiveInput::Player0;
}

void ABird::BeginPlay()
{
	Super::BeginPlay();
	
}

void ABird::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

void ABird::MoveForward(float Value)
{
	if (Controller && Value != 0.f)
	{
		FVector Forward = GetActorForwardVector();
		AddMovementInput(Forward, Value);
	}
}

void ABird::Turn(float Value)
{
	AddControllerYawInput(Value);
}

void ABird::LookUp(float Value)
{
	AddControllerPitchInput(Value);
}

// Called to bind functionality to input
void ABird::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);
	
	PlayerInputComponent->BindAxis(FConsts::MoveForward,this, &ABird::MoveForward);
	PlayerInputComponent->BindAxis(FConsts::Turn, this, &ABird::Turn);
	PlayerInputComponent->BindAxis(FConsts::LookUp, this, &ABird::LookUp);
}

'Unreal Engine > 0. Unreal 5' 카테고리의 다른 글

언리얼 Inverse Kinematic  (0) 2023.07.14
Character  (0) 2023.07.13
Pawn 움직이기  (0) 2023.07.12
Forward Declaration (전방 선언)  (0) 2023.07.12
Pawn 추가  (0) 2023.07.12
Comments