03. 플레이어 캐릭터 - Elysia-ff/UE4-Custom_Stencil_Tutorial GitHub Wiki
이제 맵을 돌아다닐 캐릭터를 생성하겠다. ACharacter
를 상속받는 AStealthCharacter
를 생성한다.
이는 맵을 돌아다닐 모든 캐릭터들의 베이스 클래스가 된다.
방금 만든 베이스 클래스를 확장해서 AHitman
을 생성하고 Blueprint BP_Hitman
을 생성한다.
BP_Hitman
에서 메시를 설정해주고 위치를 조정한다. (여기서는 엔진에서 기본제공하는 메시를 사용함)
(BP_Hitman
의 Details 패널)
Level에 끌어다 놓으면 잘 나온다.
플레이어가 BP_Hitman
을 조작하도록 할 것이기 때문에 게임 시작 시 C++ 에서 액터를 스폰한다.
StealthPlayerController.h
에 다음 코드를 추가한다.
class AHitman;
protected:
virtual void BeginPlay() override;
private:
UPROPERTY()
UClass* HitmanBlueprint;
UPROPERTY()
AHitman* Hitman;
다음은 StealthPlayerController.cpp
의 생성자에 다음 코드를 추가한다.
static ConstructorHelpers::FObjectFinder<UClass> HitmanObjectFinder(TEXT("Blueprint'/Game/Blueprint/Character/BP_Hitman.BP_Hitman_C'"));
check(HitmanObjectFinder.Succeeded());
HitmanBlueprint = HitmanObjectFinder.Object;
그리고 다음 코드를 추가한다.
#include "Character/Hitman.h"
void AStealthPlayerController::BeginPlay()
{
Super::BeginPlay();
check(HitmanBlueprint);
FActorSpawnParameters Param;
Param.Owner = this;
Param.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
Hitman = GetWorld()->SpawnActor<AHitman>(HitmanBlueprint, FVector(0.0f, 0.0f, 90.0f), FRotator::ZeroRotator, Param);
}
이제 실행하면 맵 중앙에 잘 생성된다.
/Character/StealthCharacter.h
// Copyright 2021. Elysia-ff
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "StealthCharacter.generated.h"
UCLASS()
class STEALTHTUTORIAL_API AStealthCharacter : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
AStealthCharacter(const FObjectInitializer& ObjectInitializer);
};
/Character/StealthCharacter.cpp
// Copyright 2021. Elysia-ff
#include "StealthCharacter.h"
// Sets default values
AStealthCharacter::AStealthCharacter(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
}
/Character/Hitman.h
// Copyright 2021. Elysia-ff
#pragma once
#include "CoreMinimal.h"
#include "Character/StealthCharacter.h"
#include "Hitman.generated.h"
/**
*
*/
UCLASS()
class STEALTHTUTORIAL_API AHitman : public AStealthCharacter
{
GENERATED_BODY()
};
/Player/StealthPlayerController.h
// Copyright 2021. Elysia-ff
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/PlayerController.h"
#include "StealthPlayerController.generated.h"
class AHitman;
/**
*
*/
UCLASS()
class STEALTHTUTORIAL_API AStealthPlayerController : public APlayerController
{
GENERATED_BODY()
public:
AStealthPlayerController(const FObjectInitializer& ObjectInitializer);
virtual void PlayerTick(float DeltaTime) override;
protected:
virtual void BeginPlay() override;
private:
UPROPERTY()
UClass* HitmanBlueprint;
UPROPERTY()
AHitman* Hitman;
};
/Player/StealthPlayerController.cpp
// Copyright 2021. Elysia-ff
#include "StealthPlayerController.h"
#include "Character/Hitman.h"
#include "Player/StealthPlayerCamera.h"
AStealthPlayerController::AStealthPlayerController(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
PrimaryActorTick.bCanEverTick = true;
bShowMouseCursor = true;
static ConstructorHelpers::FObjectFinder<UClass> HitmanObjectFinder(TEXT("Blueprint'/Game/Blueprint/Character/BP_Hitman.BP_Hitman_C'"));
check(HitmanObjectFinder.Succeeded());
HitmanBlueprint = HitmanObjectFinder.Object;
}
void AStealthPlayerController::PlayerTick(float DeltaTime)
{
Super::PlayerTick(DeltaTime);
AStealthPlayerCamera* PossessedPawn = GetPawn<AStealthPlayerCamera>();
if (PossessedPawn != nullptr && GEngine != nullptr)
{
FVector2D MouseInput;
if (GetMousePosition(MouseInput.X, MouseInput.Y))
{
FVector2D MoveInput;
const FIntPoint ViewportSize = GEngine->GameViewport->Viewport->GetSizeXY();
if (MouseInput.X <= PossessedPawn->EdgePixel)
{
MoveInput.Y = -1.0f;
}
else if (MouseInput.X >= ViewportSize.X - PossessedPawn->EdgePixel)
{
MoveInput.Y = 1.0f;
}
if (MouseInput.Y <= PossessedPawn->EdgePixel)
{
MoveInput.X = 1.0f;
}
else if (MouseInput.Y >= ViewportSize.Y - PossessedPawn->EdgePixel)
{
MoveInput.X = -1.0f;
}
if (!MoveInput.IsZero())
{
PossessedPawn->AddMoveInput(MoveInput);
}
}
}
}
void AStealthPlayerController::BeginPlay()
{
Super::BeginPlay();
check(HitmanBlueprint);
FActorSpawnParameters Param;
Param.Owner = this;
Param.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
Hitman = GetWorld()->SpawnActor<AHitman>(HitmanBlueprint, FVector(0.0f, 0.0f, 90.0f), FRotator::ZeroRotator, Param);
}