언리얼 튜토리얼(03) 캐릭터 동작 함수 구현

영차영차·2021년 9월 19일
1

튜토리얼

목록 보기
3/5

함수 설명

FPSCharacter.h

virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

보통 이벤트 바인딩 함수를 넣는 함수라고 생각된다.

FPSCharacter.cpp

    PlayerInputComponent->BindAxis(축 이벤트 이름,캐릭터(보통 this),실행될 함수)

로 해당되는 축 이벤트(키)가 눌리면 해당되는 함수가 실행이 된다.
Axis는 -1 ~ 1의 값(매핑때 정한 scale값)이 함수 인자로 전해진다.

   FVector Direction = FRotationMatrix(Controller-GetControlRotation()).GetScaledAxis(EAxis::X);
   AddMovementInput(Direction, Value);

Direction으로 방향을 찾고 바인딩으로 받아온 키값을 Value로 받아오는듯 하다

예제 코드

FPSCharacter.h

#pragma once

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

UCLASS()
class FPSPROJECT_API AFPSCharacter : public ACharacter
{
    GENERATED_BODY()

public:
    // 이 캐릭터의 프로퍼티 기본값을 설정합니다.
    AFPSCharacter();

protected:
    // 게임 시작 또는 스폰시 호출됩니다.
    virtual void BeginPlay() override;

public:
    // 매 프레임 호출됩니다.
    virtual void Tick( float DeltaSeconds ) override;

    // 입력에 함수성 바인딩을 위해 호출됩니다.
    virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

	// 전후 이동을 처리합니다.
    UFUNCTION()
    void MoveForward(float Value);

    // 좌우 이동을 처리합니다.
    UFUNCTION()
    void MoveRight(float Value);
};

FPSCharacter.cpp

#include "FPSCharacter.h"

// Sets default values
AFPSCharacter::AFPSCharacter()

{
// 이 캐릭터가 매 프레임 Tick() 을 호출하도록 합니다. 필요치 않을 경우 꺼서 퍼포먼스를 향상시킬 수 있습니다.
PrimaryActorTick.bCanEverTick = true;

}

// 게임 시작시 또는 스폰시 호출됩니다.
void AFPSCharacter::BeginPlay()
{
    Super::BeginPlay();

    if (GEngine)
    {
        // 5 초간 디버그 메시지를 표시합니다. (첫 인수인) -1 "Key" 값은 이 메시지를 업데이트 또는 새로고칠 필요가 없음을 나타냅니다.
        GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("We are using FPSCharacter."));
    }
}

// 매 프레임 호출됩니다.
void AFPSCharacter::Tick( float DeltaTime )
{
    Super::Tick( DeltaTime );

}

// 입력에 함수성 바인딩을 위해 호출됩니다.
void AFPSCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
{
    Super::SetupPlayerInputComponent(PlayerInputComponent);

    // "movement" 바인딩을 구성합니다.
    PlayerInputComponent->BindAxis("MoveForward", this, &AFPSCharacter::MoveForward);
    PlayerInputComponent->BindAxis("MoveRight", this, &AFPSCharacter::MoveRight);
}

void AFPSCharacter::MoveForward(float Value)
{
    // 어느 쪽이 전방인지 알아내어, 플레이어가 그 방향으로 이동하고자 한다고 기록합니다.
    FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::X);
    AddMovementInput(Direction, Value);
}

void AFPSCharacter::MoveRight(float Value)
{
    // 어느 쪽이 오른쪽인지 알아내어, 플레이어가 그 방향으로 이동하고자 한다고 기록합니다.
    FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::Y);
    AddMovementInput(Direction, Value);
}

원본

캐릭터 동작 함수 구현 튜토리얼을 내가 보기 편하게 정리해놓은 것입니다.

처음 작성하는 함수라면 축 매핑 튜토리얼을 보고 와주시기 바랍니다

profile
게임을 만들자

0개의 댓글