본문 바로가기

플레이어 이동 추가

Kwonriver 2022. 6. 13.
728x90

 

<횡스크롤 맵 탈출 게임 플레이어 이동 추가>

 

캐릭터 클래스를 상속받아 PlayerCharacher 클래스를 생성한다.

횡스크롤 이동이기 때문에 좌우 이동만 추가하면 된다.

(다만 연습용으로 상하 좌우 마우스 로테이션까지 해놓음)

점프는 스페이스키로 적용

 

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "PlayerCharacter.generated.h"

UCLASS()
class HARDTOGETGOAL_API APlayerCharacter : public ACharacter
{
	GENERATED_BODY()

public:
	// Sets default values for this characters properties
	APlayerCharacter();

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 AxisValue);
	void MoveRight(float AxisValue);
	virtual void AddControllerYawInput(float Val) override;
	virtual void AddControllerPitchInput(float Val) override;

	void ActionJumpStart();
	void ActionJumpStop();

	UPROPERTY(VisibleAnywhere)
		class USpringArmComponent *QuaterViewCameraSpringArmComponent;

	UPROPERTY(VisibleAnywhere)
		class UCameraComponent *QuaterViewCameraComponent;
};

 

생성자에서는 카메라와 관련된 컴포넌트를 추가해준다. 다만 헤더를 선언해줘야한다.

#include <Engine/Classes/Camera/CameraComponent.h>
#include <Engine/Classes/GameFramework/CharacterMovementComponent.h>
#include <Engine/Classes/GameFramework/SpringArmComponent.h>

 

APlayerCharacter::APlayerCharacter()
{
 	// Set this character to call Tick() every frame.  You can turn this off to improve performance if you dont need it.
	PrimaryActorTick.bCanEverTick = true;

	QuaterViewCameraSpringArmComponent = CreateDefaultSubobject<USpringArmComponent>(TEXT("QuaterViewCameraSpringArm"));
	QuaterViewCameraSpringArmComponent->SetupAttachment(GetCapsuleComponent());
	QuaterViewCameraSpringArmComponent->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f));
	QuaterViewCameraSpringArmComponent->TargetOffset = FVector(0.0f, 0.0f, 0.0f);
	QuaterViewCameraSpringArmComponent->ProbeSize = 1.0f;
	QuaterViewCameraSpringArmComponent->bUsePawnControlRotation = false;

	QuaterViewCameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("QuaterViewCamera"));
	QuaterViewCameraComponent->SetupAttachment(QuaterViewCameraSpringArmComponent, USpringArmComponent::SocketName);
	//QuaterViewCameraComponent->SetRelativeRotation(FRotator(0.0f, 0.0f, -50.0f));
	//QuaterViewCameraComponent->SetFieldOfView(120.0f);
}

 

구체적인 카메라 위치는 해당 클래스의 파생 블루프린트에서 세팅해준다.

그래야 수정하기가 편하더라...좌표값 일일히 수정하고 빌드하고 수정하고 빌드하고 했더니 미칠 노릇..

 

SetupPlayerInputComponent에서는 이동과 관련된 함수를 바인딩하고 캐릭터 무브먼트를 게임에 맞게 수정한다.

void APlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	//InputComponent->BindAxis("MoveForward", this, &APlayerCharacter::MoveForward);
	InputComponent->BindAxis("MoveRight", this, &APlayerCharacter::MoveForward);
	//InputComponent->BindAxis("Turn", this, &APlayerCharacter::AddControllerYawInput);
	//InputComponent->BindAxis("LookUp", this, &APlayerCharacter::AddControllerPitchInput);

	InputComponent->BindAction("Jump", IE_Pressed, this, &APlayerCharacter::ActionJumpStart);
	InputComponent->BindAction("Jump", IE_Released, this, &APlayerCharacter::ActionJumpStop);

	GetCharacterMovement()->GravityScale = 3.0f;		// 중력 크기를 키워 더욱 빨리 떨어지도록 함
	GetCharacterMovement()->JumpZVelocity = 1500.0f;	// 점프할 때 가해지는 속력, 높을수록 높이 올라감
	GetCharacterMovement()->AirControl = 0.25f;			// 공중에서 얼마나 컨트롤이 가능한지 조절
	JumpMaxCount = 1;
}

 

마우스에 의한 회전인 Turn, LoopUp은 연습용으로 주석처리함

점프는 키를 누를 때와 뗄 때를 모두 추가. 현재는 점프 중에 무언가 하는게 없지만 추가될 가능성이 있음

 

void APlayerCharacter::MoveForward(float AxisValue)
{
	AddMovementInput(GetActorForwardVector(), AxisValue * 1.2f);
}

void APlayerCharacter::MoveRight(float AxisValue)
{
	AddMovementInput(GetActorRightVector(), AxisValue * 1.2f);
}

void APlayerCharacter::ActionJumpStart()
{
	bPressedJump = true;
}

void APlayerCharacter::ActionJumpStop()
{
	bPressedJump = false;
}

 

 

위 클래스를 기반으로 블루프린트 BP_PlayerCharacter를 생성. 간단한 메시를 추가해주고 카메라 위치를 잡아준다.

 

 

 

게임모드를 상속받아 간단한 게임모드를 만들어주고 월드세팅에서 게임모드와 Defualt Pawn Class를 변경한다

 

 

플레이하면 자동으로 BP_PlayerCharacter 오브젝트가 생성되며 키 입력을 받아 이동할 수 있다.

728x90

'[Unreal4] > 횡스크롤 맵탈출' 카테고리의 다른 글

밟으면 사라지는 바닥 추가  (0) 2022.06.17
횡으로 이동하는 바닥 추가  (0) 2022.06.14