Mini

게임플레이 프레임워크 본문

UE5

게임플레이 프레임워크

Mini_96 2022. 8. 7. 17:35

* GameModeBase

: 게임규칙, 게임의 신 역할

ex) 롤에서 넥서스터지면 게임끝

 

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

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/GameModeBase.h"
#include "MyGameModeBase.generated.h"

/**
 * 
 */
UCLASS()
class TESTUNREALENGINE_API AMyGameModeBase : public AGameModeBase
{
	GENERATED_BODY()

	AMyGameModeBase();
};
// Fill out your copyright notice in the Description page of Project Settings.


#include "MyGameModeBase.h"
#include "MyPawn.h"

//기본생성자
AMyGameModeBase::AMyGameModeBase()
{
	DefaultPawnClass = AMyPawn::StaticClass();
}

생성자만듬 && 기본플레이어(가 사용할폰)을 MyPawn으로 설정

 

* MyPawn

:입력을 받을 수 있는 클래스

유니티 : update함수에 입력처리O => 종속성(-)

언리얼 : tick함수에 입력처리X, SetupPlayerInputComponent 함수에 입력부분 따로처리

 

* 바인딩 종류

액션 : 조이스틱 버튼

축(axis) : 조이스틱 손잡이

 

- WASD로 움직이도록 구현

// Called to bind functionality to input
void AMyPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	PlayerInputComponent->BindAxis(TEXT("UpDown"), this, &AMyPawn::UpDown);
	PlayerInputComponent->BindAxis(TEXT("LeftRight"), this, &AMyPawn::LeftRight);
}

void AMyPawn::UpDown(float Value)
{
	UE_LOG(LogTemp, Warning, TEXT("UpDown %f"), Value);
}

void AMyPawn::LeftRight(float Value)
{
	UE_LOG(LogTemp, Warning, TEXT("LeftRight %f"), Value);
}

1. MyPawn에서 작업

PlayerInputComponent->BindAxis(엔진과 매핑될 이름,대상, 실행할함수)

2. 세팅-프로젝트설정-입력 설정

Scale값이 코드의 Value로 들어온다.

 

- 움직임 구현

유니티 : transform.positon += asdfsdaf ... 

단점 : 유동적 대응이 힘들다. ex)물위에서 걸을때, 땅에서 걸을때 등등

언리얼: UFloatingPawnMovement 클래스 에서 관리

void AMyPawn::UpDown(float Value)
{
	//UE_LOG(LogTemp, Warning, TEXT("UpDown %f"), Value);
	AddMovementInput(GetActorForwardVector(), Value);
}

void AMyPawn::LeftRight(float Value)
{
	//UE_LOG(LogTemp, Warning, TEXT("LeftRight %f"), Value);
	AddMovementInput(GetActorRightVector(), Value);
}

AddMovementInput(방향,스케일) : 키보드 S누르면 -1(엔진설정값)이 곱해짐 => 뒤로가게됨.

 

 

- 빌드오류

원인 : CoreMinimal.h에 UFloatingPawnMovement 가 포함안되있음

해결 : mypawn.h에 전방선언 && .cpp에 include

private:
	UPROPERTY(VisibleAnywhere);	//포인터타입 : visible->밖의 그래픽으로 조정가능
	UStaticMeshComponent* Mesh;

	UPROPERTY(VisibleAnywhere);
	class UFloatingPawnMovement* movement;
#include "GameFramework/FloatingPawnMovement.h"

 

- 완성 코드

MyPawn.h

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

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "MyPawn.generated.h"

UCLASS()
class TESTUNREALENGINE_API AMyPawn : public APawn
{
	GENERATED_BODY()

public:
	// Sets default values for this pawn's properties
	AMyPawn();

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 UpDown(float Value);
	void LeftRight(float Value);
	

private:
	UPROPERTY(VisibleAnywhere);	//포인터타입 : visible->밖의 그래픽으로 조정가능
	UStaticMeshComponent* Mesh;

	UPROPERTY(VisibleAnywhere);
	class UFloatingPawnMovement* Movement; 

};

MyPawn.cpp

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


#include "MyPawn.h"
#include "GameFramework/FloatingPawnMovement.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>(TEXT("MESH"));
	Movement = CreateDefaultSubobject<UFloatingPawnMovement>(TEXT("MOVEMENT"));


	RootComponent = Mesh;

	static ConstructorHelpers::FObjectFinder<UStaticMesh> SM(TEXT("StaticMesh'/Game/StarterContent/Props/SM_Couch.SM_Couch'"));

	if (SM.Succeeded())
		Mesh->SetStaticMesh(SM.Object);

}

// 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(TEXT("UpDown"), this, &AMyPawn::UpDown);
	PlayerInputComponent->BindAxis(TEXT("LeftRight"), this, &AMyPawn::LeftRight);
}

void AMyPawn::UpDown(float Value)
{
	if (Value == 0.f) return;
	UE_LOG(LogTemp, Warning, TEXT("UpDown %f"), Value);
	AddMovementInput(GetActorForwardVector(), Value);
}

void AMyPawn::LeftRight(float Value)
{
	if (Value == 0.f) return;
	UE_LOG(LogTemp, Warning, TEXT("LeftRight %f"), Value);
	AddMovementInput(GetActorRightVector(), Value);
}

 

 

 

 

 

* 팁 : 컨트롤+N 맵설정, 세이브로저장, 세팅->맵->기본맵으로 설정

 

 

 

'UE5' 카테고리의 다른 글

스테이트 머신  (0) 2022.08.14
애니메이션 기초  (0) 2022.08.14
블루프린트 클래스  (0) 2022.08.11
캐릭터 생성  (0) 2022.08.08
로그와 디버깅  (0) 2022.08.07