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

6.캐릭터의 제작과 컨트롤_1)캐릭터 제작

현구구 2022. 3. 4. 18:39

캐릭터와 폰의 차이점

1.점프와 같은 중력을 반영한 움직임 제공

2.기어가기 날아가기 등 다양한 움직임 설정 가능

3.캐릭터들의 움직임 자동 동기화

디폴트폰이 폰일경우 중력의 영향을 받지 않음
디폴트폰이 캐릭터일경우 중력의 영향을 받음

새로운 클래스에서 Character 를 선택하고 이름은 ABCharater으로 한다

 

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
<ABCharacter.h>
// Fill out your copyright notice in the Description page of Project Settings.
 
#pragma once
 
#include "ArenaBattle.h" // = EngineMinimal.h
#include "GameFramework/Character.h"
#include "ABCharacter.generated.h"
 
UCLASS()
class ARENABATTLE_API AABCharacter : public ACharacter
{
    GENERATED_BODY()
 
public:
    // Sets default values for this character's properties
    AABCharacter();
 
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=Camera)
    USpringArmComponent* SpringArm;
 
    UPROPERTY(VisibleAnywhere, Category = Camera)
    UCameraComponent* Camera;
 
private:
    void UpDown(float NewAxisValue);
    void LeftRight(float NewAxisValue);
};
 
cs

h파일에서 EngineMinimal.h 가 있는 ArenaBattle.h 를 참조해주고 

Capsule,Mesh,Movement,SpringArm,Camera 변수를 선언해주었던 ABPawn과는 다르게

SpringArm,Camera만을 선언해준다

(AABCharacter 가 상속받고있는 ACharacter파일에서 Mesh,Capsule,Movement에 대해 정의가 이미 되어있기 때문)

그리고 ABCharacter.cpp파일로 가서 ABPawn 파일에서(Mesh,Capsule,Movement) 변수들을 제외하고 거의 동일하게 코드를 작성해준다.

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<ABCharacter.cpp>
// Fill out your copyright notice in the Description page of Project Settings.
 
 
#include "ABCharacter.h"
 
// Sets default values
AABCharacter::AABCharacter()
{
     // 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;
    SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("SPRINGARM"));
    Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("CAMERA"));
 
    SpringArm->SetupAttachment(GetCapsuleComponent());
    Camera->SetupAttachment(SpringArm);
    //캐릭터는 폰의 Mesh 와 다르게 제공되는 GetMesh() 사용
    GetMesh()->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())
    {
        GetMesh()->SetSkeletalMesh(SK_CARDBOARD.Object);
    }
    GetMesh()->SetAnimationMode(EAnimationMode::AnimationBlueprint);
 
    static ConstructorHelpers::FClassFinder<UAnimInstance>WARRIOR_ANIM(TEXT("/Game/Book/Animations/WarriorAnimBlueprint.WarriorAnimBlueprint_C"));
    if (WARRIOR_ANIM.Succeeded())
    {
        GetMesh()->SetAnimInstanceClass(WARRIOR_ANIM.Class);
    }
}
 
// Called when the game starts or when spawned
void AABCharacter::BeginPlay()
{
    Super::BeginPlay();
    
}
 
// Called every frame
void AABCharacter::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);
 
}
 
// Called to bind functionality to input
void AABCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
    Super::SetupPlayerInputComponent(PlayerInputComponent);
    //BeginPlay() 보다 먼저 실행되는 함수 결합할 때 사용
    PlayerInputComponent->BindAxis(TEXT("UpDown"), this&AABCharacter::UpDown);
    PlayerInputComponent->BindAxis(TEXT("LeftRight"), this&AABCharacter::LeftRight);
}
 
void AABCharacter::UpDown(float NewAxisValue)
{
    AddMovementInput(GetActorForwardVector(), NewAxisValue);
}
 
void AABCharacter::LeftRight(float NewAxisValue)
{
    AddMovementInput(GetActorRightVector(), NewAxisValue);
}
 
 
cs

이때 Mesh에 애니메이션과 캐릭터 매쉬를 입힐 때 Mesh 변수를 선언하고 사용했던 Pawn과 달리

GetMesh()를 사용한다

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

그리고 ABGameMode.cpp에서 디폴트 폰을 방금 제작한 ABCharcter로 변경해주고 컴파일해주면 캐릭터가 중력의 영향을 받는 것을 볼 수 있다.