|
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
|
<ABCharacter.h>
#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;
enum class EControlMode
{
GTA,
DIABLO
}; //GTA와 DIABLO를 변수로 가질수 있는 EControlMode라는 자료형을 만든다
void SetControlMode(EControlMode NewControlMode);
//자료형이 EControlMode인 NewControlMode를 선언하고 NewControlMode에 따라 게임모드를 변환시킨다
EControlMode CurrentControlMode = EControlMode::GTA;//게임이 시작할때는 GTA모드로 초기화한다
FVector DirectionToMove = FVector::ZeroVector;
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);
void LookUp(float NewAxisValue);
void Turn(float NewAxisValue);
};
|
cs |
나중에 GTA 게임모드와 DIABLO 게임모드 변경을 위해 EControlMode를 선언한다
|
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
<ABCharcter.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);
}
SetControlMode(EControlMode::DIABLO); // 현재게임모드 DIABLO
}
// Called when the game starts or when spawned
void AABCharacter::BeginPlay()
{
Super::BeginPlay();
}
void AABCharacter::SetControlMode(EControlMode NewControlMode)
{
CurrentControlMode = NewControlMode;
switch(CurrentControlMode)
{
case EControlMode::GTA:
SpringArm->TargetArmLength = 450.0f; //현재 컨트롤 모드가 GTA일때 스프링암의 길이는 450
SpringArm->SetRelativeRotation(FRotator::ZeroRotator);
SpringArm->bUsePawnControlRotation = true;
SpringArm->bInheritPitch = true;
SpringArm->bInheritRoll= true;
SpringArm->bInheritYaw = true;
bUseControllerRotationYaw = false;
GetCharacterMovement()->bOrientRotationToMovement = true;//방향에따른 움직임 설정(회전기능)
GetCharacterMovement()->bUseControllerDesiredRotation = false;
GetCharacterMovement()->RotationRate = FRotator(0.0f,720.0f,0.0f);//회전속도 설정해서 자연스러운 회전 움직임
break;
case EControlMode::DIABLO:
SpringArm->TargetArmLength = 800.0f;
SpringArm->SetRelativeRotation(FRotator(-45.0f,0.0f,0.0f));
SpringArm->bUsePawnControlRotation = false;//시점 고정
SpringArm->bInheritPitch = false;
SpringArm->bInheritRoll = false;
SpringArm->bInheritYaw = false;
SpringArm->bDoCollisionTest = false;
bUseControllerRotationYaw = false;
GetCharacterMovement()->bOrientRotationToMovement = false;
GetCharacterMovement()->bUseControllerDesiredRotation = true;
GetCharacterMovement()->RotationRate = FRotator(0.0f, 720.0f, 0.0f);
break;
}
}
// Called every frame
void AABCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
switch (CurrentControlMode)
{
case EControlMode::DIABLO:
if (DirectionToMove.SizeSquared() > 0.0f)
{
GetController()->SetControlRotation(FRotationMatrix::MakeFromX(DirectionToMove).Rotator());
AddMovementInput(DirectionToMove);
}
break;
}
}
// 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);
PlayerInputComponent->BindAxis(TEXT("LookUp"), this, &AABCharacter::LookUp);
PlayerInputComponent->BindAxis(TEXT("Turn"), this, &AABCharacter::Turn);
}
void AABCharacter::UpDown(float NewAxisValue)
{
switch (CurrentControlMode)
{
case EControlMode::GTA:
AddMovementInput(FRotationMatrix(GetControlRotation()).GetUnitAxis(EAxis::X), NewAxisValue);
break;
case EControlMode::DIABLO:
DirectionToMove.X = NewAxisValue;
break;
}
}
void AABCharacter::LeftRight(float NewAxisValue)
{
switch (CurrentControlMode)
{
case EControlMode::GTA:
AddMovementInput(FRotationMatrix(GetControlRotation()).GetUnitAxis(EAxis::Y), NewAxisValue);
break;
case EControlMode::DIABLO:
DirectionToMove.Y = NewAxisValue;
break;
}
}
void AABCharacter::LookUp(float NewAxisValue)
{
switch (CurrentControlMode)
{
case EControlMode::GTA:
AddControllerPitchInput(NewAxisValue);
break;
}
}
void AABCharacter::Turn(float NewAxisValue)
{
switch (CurrentControlMode)
{
case EControlMode::GTA:
AddControllerYawInput(NewAxisValue);
break;
}
}
|
cs |
완성된 cpp파일
현재 게임모드를 DIABLO로 하고 게임을 실행하면 게임모드가 바뀐것을 볼 수 있다.
'언리얼 개인 프로젝트 > 언리얼엔진C++ 독학(이득우)' 카테고리의 다른 글
| 7.애니메이션 시스템의 설계_1)달리기와 멈춤 애니메이션 (0) | 2022.03.05 |
|---|---|
| 6.캐릭터 제작과 컨트롤_5)컨트롤 방식 변경 (0) | 2022.03.05 |
| 6.캐릭터 제작과 컨트롤_3)GTA방식 컨트롤 구현 (0) | 2022.03.04 |
| 6.캐릭터 제작과 컨트롤_2)캐릭터 회전 (0) | 2022.03.04 |
| 6.캐릭터의 제작과 컨트롤_1)캐릭터 제작 (0) | 2022.03.04 |