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

6.캐릭터 제작과 컨트롤_3)GTA방식 컨트롤 구현

현구구 2022. 3. 4. 22:13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<ABCharacter.h>
 
#pragma once
...
 
UCLASS()
class ARENABATTLE_API AABCharacter : public ACharacter
{
    ...
protected:
    ...
 
    void SetControlMode(int32 ControlMode);
...
};
 
cs

SetControlMode()함수를 만들어 괄호 안의 변수에 따라 게임모드를 바꾸는 기능을 만든다

 

 

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
<ABCharcter.cpp>
 
#include "ABCharacter.h"
 
// Sets default values
AABCharacter::AABCharacter()
{
     ...
    SetControlMode(0); // ControlMode == 0 
}
 
// Called when the game starts or when spawned
...
void AABCharacter::SetControlMode(int32 ControlMode)
{
    if (ControlMode == 0)
    {
        SpringArm->TargetArmLength = 450.0f; //컨트롤 모드가 0일때 스프링암의 길이는 450
        SpringArm->SetRelativeRotation(FRotator::ZeroRotator);
        SpringArm->bUsePawnControlRotation = true;
        SpringArm->bInheritPitch = true;
        SpringArm->bInheritRoll= true;
        SpringArm->bInheritYaw = true;
        bUseControllerRotationYaw = false;
    }
}
 
...
 
 
cs

 

카메라의 설정만을 바꿨기 때문에 w(앞)키를 눌렀을 때 카메라 시점이 뒤를 향하고 있음에도 불구하고 앞으로 향하는 모습을 보여준다

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<ABCharacter.cpp>
 
 
#include "ABCharacter.h"
 
...
 
void AABCharacter::UpDown(float NewAxisValue)
{
    AddMovementInput(FRotationMatrix(GetControlRotation()).GetUnitAxis(EAxis::X), NewAxisValue);
}
 
void AABCharacter::LeftRight(float NewAxisValue)
{
    AddMovementInput(FRotationMatrix(GetControlRotation()).GetUnitAxis(EAxis::Y), NewAxisValue);
}
 
...
cs

 

카메라시점을 받아 카메라 시점대로 이동하는 코드는 다음과 같다.

이때 EAxis::X는 캐릭터의 시선방향, EAxis::Y는 캐릭터의 우측방향이다.

 

이제 W(앞)키만 눌렀을 뿐인데 카메라 시점에 따라 캐릭터가 이동하는 모습을 보여준다.