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

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

현구구 2022. 3. 4. 16:05

 

언리얼 에디터에서 세팅>프로젝트 세팅>엔진-입력으로 가면 액션 매핑과 축 매핑이 있는것을 볼 수 있다.

축 매핑은 방향전환과 같은 키 설정이고 액션 매핑은 점프나 공격같은 키 설정이다.

축 매핑에 UpDown 과 LeftRight 를 추가하고 UpDown에는 W/S 키를, LeftRight에는 A/D키를 입력한다.

앞과 오른쪽인 W 와 D 에는 스케일에 1값을 , 뒤와 왼쪽인 S 와 A 에는 스케일에 -1값을 넣는다

 

1
2
3
4
5
6
7
8
9
10
11
<ABPawn.h>
 
class ARENABATTLE_API AABPawn : public APawn
{
    GENERATED_BODY()
 ... 
private:
    void UpDown(float NewAxisValue);
    void LeftRight(float NewAxisValue);
};
 
cs

그리고 ABPawn.h 파일에 선언해준다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<ABPawn.cpp>
 
void AABPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
    Super::SetupPlayerInputComponent(PlayerInputComponent);
 
    PlayerInputComponent->BindAxis(TEXT("UpDown"), this&AABPawn::UpDown);
    PlayerInputComponent->BindAxis(TEXT("LeftRight"), this&AABPawn::LeftRight);
}
void AABPawn::UpDown(float NewAxisValue)
{
    AddMovementInput(GetActorForwardVector(), NewAxisValue);
}
 
void AABPawn::LeftRight(float NewAxisValue)
{
    AddMovementInput(GetActorRightVector(), NewAxisValue);
}
//AddMovementInput을 사용해 상하좌우로 조종하는 코드 
csa

SetupPlaSetupPlayerInputyerInputComponent 함수는 BeginPlay함수보다도 먼저 작동하는 함수로 기능을 Pawn 에 묶을 때 사용한다

이곳에 UpDown와 LeftRight를 Pawn 에 묶어주고SetupPlayerInput h파일에서 선언한 UpDown와 LeftRight 함수를 정의해준다

컴파일하고 게임을 실행해보면 wasd 키에 따라 폰이 움직이는걸 볼 수 있다