언리얼 개인 프로젝트/언리얼엔진C++ 독학(이득우)

5. 폰의 제작과 조작_1폰제작

현구구 2022. 3. 4. 15:21

새로운 c++클래스에서 Gamemode 를 선택 하고 ABGamemode로 파일을 하나 만든다

그 다음 ABGameMode 에서 사용할 캐릭터 Pawn을 ABPawn을 하나 만들고

ABGameMode에서 사용할 플레이어 컨트롤러를 ABPlayerController로 하나 만들어준다.

그리고 월드 세팅에서 게임모드 오버라이드에서 방금 만든 ABGameMode로 설정한다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<ABGameMode.h>
 
// Fill out your copyright notice in the Description page of Project Settings.
 
#pragma once
 
#include "ArenaBattle.h"
#include "GameFramework/GameModeBase.h"
#include "ABGameMode.generated.h"
 
/**
 * 
 */
UCLASS()
class ARENABATTLE_API AABGameMode : public AGameModeBase
{
    GENERATED_BODY()
    
public:
    AABGameMode(); //생성자 선언
};
 
cs

 

우선 ABGameMode.h 파일에서 생성자를 선언한다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<ABGameMode.cpp>
 
// Fill out your copyright notice in the Description page of Project Settings.
 
 
#include "ABGameMode.h"
#include "ABPawn.h" //ABGameMode 에서 사용할 파일들을 참조
#include "ABPlayerController.h"
 
AABGameMode::AABGameMode() //생성자 정의 
{
    DefaultPawnClass = AABPawn::StaticClass(); //이 게임모드에서 디폴트 폰을 AABPawn 으로 지정해준다
    PlayerControllerClass = AABPlayerController::StaticClass();
}
 
cs

cpp파일에서 ABPawn 과 ABPlayerController 해더파일들을 include 해준다

그 다음 이 게임모드에서 사용한 디폴트 폰과 플레이어 컨트롤러는 방금 만든 ABPawn와 ABController로 초기화해준다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<ABPawn.h>
// Fill out your copyright notice in the Description page of Project Settings.
 
#pragma once
 
#include "ArenaBattle.h" //  =EngineMinimal.h
#include "GameFramework/Pawn.h"
#include "GameFramework/FloatingPawnMovement.h"//폰의 움직임에 필요한 함수들을 담은 해더파일
#include "ABPawn.generated.h"
 
UCLASS()
class ARENABATTLE_API AABPawn : public APawn
{
    GENERATED_BODY()
 
public:
    // Sets default values for this pawn's properties
    AABPawn();
 
protected:
    // Called when the game starts or when spawned
    virtual void BeginPlay() override;
 
public:    
    // Called every frame
    virtual void Tick(float DeltaTime) override;
 
    // Called to bind functionality to input
    virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
 
    UPROPERTY(VisibleAnywhere, Category=Collision)
    UCapsuleComponent* Capsule; // 폰의 충돌 영역 설정
 
    UPROPERTY(VisibleAnywhere, Category = Visual)
    USkeletalMeshComponent* Mesh; //폰의 외형, 애니메이션 설정
 
    UPROPERTY(VisibleAnywhere, Category = Movement)
    UFloatingPawnMovement* Movement; //폰의 움직임 설정
 
    UPROPERTY(VisibleAnywhere, Category = Camera)
    USpringArmComponent* SpringArm; //카메라 구도 설정 //USpringArmComponent선언할때 EngineMinimal.h필요
 
    UPROPERTY(VisibleAnywhere, Category = Camera)
    UCameraComponent* Camera; //카메라 설정 
};
 
cs

폰을 설정하기위해 필요한 변수들을 ABPawn의 해더파일에 선언해준다

변수 선언을 위해 "ArenaBattle.h" "GameFramework/FloatingPawnMovement.h"를 include 해준다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<ABPawn.cpp>
 
AABPawn::AABPawn()
{
     // Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    PrimaryActorTick.bCanEverTick = true;
 
    Capsule = CreateDefaultSubobject<UCapsuleComponent>(TEXT("CAPSULE"));
    Mesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("MESH"));
    Movement = CreateDefaultSubobject<UFloatingPawnMovement>(TEXT("MOVEMENT"));
    SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("SPRINGARM"));
    Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("CAMERA"));
 
    RootComponent = Capsule; //루트 컴포넌트는 Capsule로 설정
    Mesh->SetupAttachment(Capsule);
    SpringArm->SetupAttachment(Capsule);
    Camera->SetupAttachment(SpringArm);
 
    Capsule->SetCapsuleHalfHeight(88.0f);//Capsule 캐릭터의 절반높이를 88로 설정
    Capsule->SetCapsuleRadius(34.0f); // 반지름은 34로 설정
    Mesh->SetRelativeLocationAndRotation(FVector(0.0f, 0.0f, -88.0f), FRotator(0.0f, -90.0f, 0.0f));
    //매쉬는 발바닥 기준으로 위치를 잡기 때문에 위치를 -88내리고 y축으로 90도 회전시켜 폰에 매쉬를 맞춘다.
    SpringArm->TargetArmLength = 400.0f;
    SpringArm->SetRelativeRotation(FRotator(-15.0f,0.0f,0.0f));
 
    static ConstructorHelpers::FObjectFinder<USkeletalMesh>SK_CARDBOARD(TEXT("/Game/InfinityBladeWarriors/Character/CompleteCharacters/SK_CharM_Cardboard.SK_CharM_Cardboard"));
    if (SK_CARDBOARD.Succeeded())
    {
        Mesh->SetSkeletalMesh(SK_CARDBOARD.Object);
    }
}
 
cs

그리고 ABPawn 생성자에서 해더파일에서 선언한 변수들을 설정해준다.

마지막으로 ABPawn 에 입힐 스켈레탈 매쉬의 경로를 복붙해서 입혀준다

컴파일하고 게임 실행했을때 ABPlayerController코드를 완성하지 않아 아직 움직이지 않지만 폰이 생성된 모습을 볼 수 있다.