'Unreal C++/C++ Pawn'에 해당되는 글 1건

  1. 2018.08.22 Unreal Engine C++ Pawn example
Unreal C++/C++ Pawn2018. 8. 22. 18:49

언리얼 엔진에서 C++를 이용한 Pawn 클래스 기반으로 커스텀 폰 클래스 정의하기


언리얼 엔진에서 Actor와 Pawn이 다른 점은 Pawn은 AddMovementInput()함수를 사용하여 전후좌우상하로 이동할 수 있으므로 키보드의 입력을 Pawn에 반영할 수 있다는 것이다


여기서는 C++를 사용하여 Pawn 기반의 MyPawn 클래스를 생성하고 키보드(W, A, S, D)키를 누를 때마다 해당 폰이 전후좌우로 이동하는 기능을 작성해보고자 한다


C++ 프로젝트 생성

Pawn 기반 C++ 클래스 생성(MyPawn)



.h 헤더파일 ( 적색 코드는 추가한 내용)

// Fill out your copyright notice in the Description page of Project Settings.


#pragma once


#include "CoreMinimal.h"

#include "GameFramework/Pawn.h"

#include "Components/InputComponent.h"

#include "GameFramework/FloatingPawnMovement.h"

#include "Camera/CameraComponent.h"

#include "MyPawn.generated.h"


UCLASS()

class CPP_PAWN_TEST_API AMyPawn : public APawn

{

GENERATED_BODY()


public:

// Sets default values for this pawn's properties

AMyPawn();


UPROPERTY(EditAnywhere)

UStaticMeshComponent* Mesh;


UPROPERTY(EditAnywhere)

UCameraComponent* Camera;


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;


void MoveForward(float Value);

void MoveRight(float Value);

};



.cpp 파일 (적색 코드는 추가된 내용)

// Fill out your copyright notice in the Description page of Project Settings.


#include "MyPawn.h"


// Sets default values

AMyPawn::AMyPawn()

{

  // Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.

PrimaryActorTick.bCanEverTick = true;


Mesh = CreateDefaultSubobject<UStaticMeshComponent>("MyMesh");

CreateDefaultSubobject<UFloatingPawnMovement>("PawnMovement");

Camera = CreateDefaultSubobject<UCameraComponent>("MyCamera");

}


// Called when the game starts or when spawned

void AMyPawn::BeginPlay()

{

Super::BeginPlay();

}


// Called every frame

void AMyPawn::Tick(float DeltaTime)

{

Super::Tick(DeltaTime);

}


// Called to bind functionality to input

void AMyPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)

{

Super::SetupPlayerInputComponent(PlayerInputComponent);

PlayerInputComponent->BindAxis("MoveForward", this, &AMyPawn::MoveForward);

PlayerInputComponent->BindAxis("MoveRight", this, &AMyPawn::MoveRight);

}


void AMyPawn::MoveForward(float Value)

{

AddMovementInput(GetActorForwardVector(), Value);

}


void AMyPawn::MoveRight(float Value)

{

AddMovementInput(GetActorRightVector(), Value);

}



Axis Mappings

언리얼 에디터에서 Edit > 프로젝트 세팅 > 엔진 > 입력 > Axis Mappings

MoveForward 라는 바인딩 이름으로 W(1.0), S(-1.0) 등록

MoveRight 라는 바인딩 이름으로  D(1.0), A(-1.0) 키를 등록



실행 테스트

완성된 C++ MyPawn 클래스를 Content Browser에서 드래그하여 뷰포트에 놓는다

컴포넌트로 포함된 카메라의 위치와 각도를 언리얼 에디터에서 변경하려면 Camera 변수에 매크로UFUNCTION(EditAnywhere0 를 설정해 주어야 한다

뷰포트에서 MyPawn을 선택하고 [디테일] 뷰에서 Mesh 변수를 선택한 후 Static Mesh 패널에서 Cube 등 임의의 메시를 선택하여 설정한다


게임을 실행하여 W, S, A, D 키를 이용하여 이동할 수 있는지 확인한다

만약 카메라만 이동하고 메시가 이동하지 않으면, Mesh 변수를 선택하고 [디테일]뷰의 Pawn 패널에서 Auto Possess Player 항목에 Disabled 가 아닌 Player0를 설정하면 된다


Posted by cwisky