이 튜토리얼에서는 캐릭터를 생성하고 입력을 받도록 구성한 다음, 게임플레이 때 기본 폰으로 사용할 캐릭터를 게임 모드에 할당합니다. 캐릭터를 생성한 후에는 캐릭터가 플레이어 입력에 어떻게 반응할지 정의합니다.
언리얼은 다양한 프로젝트 유형에 맞춰 더 정교한 입력 매핑을 제공합니다. 자세한 내용은 향상된 입력 문서를 참조하세요.
프로젝트 구성
Create a new Games > Blank > C++ project named "SettingUpInput".
In the Editor, navigate to Edit > Project Settings > Input > Bindings.
액션 매핑 및 축 매핑 구성
입력 정의는 액션 매핑(Action Mappings)과 축 매핑(Axis Mappings)의 사용자 정의 바인딩(Bindings)을 통해 이뤄집니다. 두 매핑은 입력 행동과 호출하는 키 사이에 인디렉션 레이어를 삽입하여 키와 축을 입력 행동에 간편하게 매핑하는 메커니즘을 제공합니다.
액션 매핑은 키 누르기와 놓기를, 축 매핑은 지속적인 범위를 갖는 입력을 허용합니다. 정의한 매핑은 블루프린트나 C++의 행동에 바인딩할 수 있습니다.
액션 매핑(Action Mappings) 옆의 추가(+)를 클릭하여 Jump라는 이름의 액션을 새로 생성합니다.
드롭다운 화살표(1) 또는 키 값 선택(Select Key Value) 버튼(2) 중 하나에서 스페이스 바(Space Bar) 키 값을 검색하여 선택합니다.
축 매핑(Axis mappings)에서 추가(+)를 클릭하여 다음 축 매핑 이름, 키(Key) 값, 스케일(Scale) 값을 생성합니다.
축 매핑 이름 키 값 스케일 값 MoveForward
W
1.0
S
-1.0
MoveRight
A
-1.0
D
1.0
Turn
마우스 X
1.0
룩업
마우스 Y
-1.0
샘플 캐릭터 생성
A Character is a special type of Pawn that has the ability to walk around. Characters extend from the Pawn class, and inherit similar properties such as physical representation of a player or AI entity within the world.
From the Content Drawer, navigate to the C++ classes folder, right-click and select New C++ Class, then choose Character as your parent class.
Name your character class "ExampleCharacter", then click Create Class.
스프링 암 및 카메라 컴포넌트 생성하기
When the Camera and SpringArm Components are used together, they provide functionality for a third-person perspective that can dynamically adjust to your game world. The camera component contains a camera that represents the player's point of view or how the player sees the world. The SpringArm component is used as a "camera boom" to keep the camera for a player from colliding into the world.
In your code editor, navigate to ExampleCharacter.h. In the Class defaults, declare the following classes.
C++protected: UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Components") class USpringArmComponent* CameraBoom; UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Components") class UCameraComponent* FollowCamera; |UProperty Specifiers are used to provide visibility of the component in the Blueprint Editor.
Navigate to your
ExampleCharacter.cppfile. Add the following libraries to the include line.C++#include "GameFramework/SpringArmComponent.h" #include "Camera/CameraComponent.h"Next, implement the following in the
AExampleCharacterconstructor.C++AExampleCharacter::AExampleCharacter() { //Initialize the Camera Boom CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom")); //Setup Camera Boom attachment to the Root component of the class CameraBoom->SetupAttachment(RootComponent); //Set the boolean to use the PawnControlRotation to true. CameraBoom->bUsePawnControlRotation = true;The component calls the FObjectInitializer::CreateDefaultSubobjecttemplate, then uses the SetupAttachment method to attach to a parent Scene Component. When setting the Camera Boom to use the Pawn's control rotation, it uses its parent pawn's rotation instead of its own.
Compile your code.
Creating the Action/Axis Functions to your Input Component
In your
ExampleCharacter.hclass defaults, declare the following Input functions.C++protected: void MoveForward(float AxisValue); void MoveRight(float AxisValue);Navigate to your
ExampleCharacter.cppand implement yourMoveForwardandMoveRightmethods.C++void AExampleCharacter::MoveForward(float AxisValue) { if ((Controller != NULL) && (AxisValue != 0.0f)) { // find out which direction is forward const FRotator Rotation = Controller->GetControlRotation(); const FRotator YawRotation(0, Rotation.Yaw, 0); // get forward vector const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);Navigate to the SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) method, then implement the following code.
C++void AExampleCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) { Super::SetupPlayerInputComponent(PlayerInputComponent); PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump); PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping); PlayerInputComponent->BindAxis("MoveForward", this, &AExampleCharacter::MoveForward); PlayerInputComponent->BindAxis("MoveRight", this, &AExampleCharacter::MoveRight); PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);The Player Input Component links the AxisMappings and ActionMappings in your project to game actions. Both the Pawn and Character class contain methods that are inherited and can be used or extended for your custom characters. In our example, we've used the Pawn's AddControllerYawInput and AddControllerPitchInput functions, and the Character's Jump and StopJumping functions.
Compile your code.
Finished Code
ExampleCharacter.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "ExampleCharacter.generated.h"
UCLASS()
class SETTINGUPINPUT_API AExampleCharacter : public ACharacter
{
GENERATED_BODY()
ExampleCharacter.cpp
// Sets default values
AExampleCharacter::AExampleCharacter()
{
//Initialize the Camera Boom
CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
//Setup its attachment to the Root component of the class
CameraBoom->SetupAttachment(RootComponent);
//Set the boolean to use the PawnControlRotation to true.
Creating the Character Blueprint
Navigate to your C++ Classes Folder and right click your ExampleCharacter, from the drop down menu select Create Blueprint class based on ExampleCharacter. Name your Blueprint BP_ExampleCharacter.
In the Components panel, select the Mesh Skeletal Mesh Component.
Navigate to Details > Mesh > Skeletal Mesh and expand the drop-down menu. In the Browser section, click the Settings Icon. Then from the context menu, select Content > Show Engine Content.
Search for and select the TutorialTPP Skeletal Mesh.
Navigate to the Transform category, then set the Location and Rotation vector values to (0.0, 0.0, -90)
게임 모드 블루프린트 생성
The GameMode defines the game's set of rules. These rules include what default pawn the player will spawn when the game is launched. You need to set up these rules to spawn the Player Character you created.
In the Content Drawer, navigate to your C++ Classes folder, right-click the SettingUpInputGameModeBase, then in the drop-down menu select Create Blueprint Based on SettingUpInputGameModeBase. Name your game mode Blueprint "BP_InputGameMode".
In the Class defaults, navigate to Classes > Default Pawn Class, and select the BP_ExampleCharacter.
Compile and Save.
Navigate to Edit > Project Settings > Maps and Modes. Set the Default GameMode to BP_InputGameMode.
Navigate to the Editor and select Play (Play in Editor)
You can now control your character's movement using the W, A, S, D keys. Moving the mouse moves the camera, and pressing the spacebar causes the character to jump.