언리얼 튜토리얼(05) 캐릭터 점프 구현

함수 추가

FPSCharacter.h

// 키를 누르면 점프 플래그를 설정합니다.
UFUNCTION()
void StartJump();

// 키를 떼면 점프 플래그를 지웁니다.
UFUNCTION()
void StopJump();

FPSCharacter.cpp

void AFPSCharacter::StartJump()
{
    bPressedJump = true;
}

void AFPSCharacter::StopJump()
{
    bPressedJump = false;
}

SetupPlayerInputComponent()에 추가 ↓

// "action" 바인딩을 구성합니다.
PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &AFPSCharacter::StartJump);
PlayerInputComponent->BindAction("Jump", IE_Released, this, &AFPSCharacter::StopJump)

예제 코드

FPSCharacter.h

#pragma once

#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);

    // 키를 누르면 점프 플래그를 설정합니다.
    UFUNCTION()
    void StartJump();

    // 키를 누르면 점프 플래그를 지웁니다.
    UFUNCTION()
    void StopJump();
};

FPSCharacter.cpp

#include "FPSProject.h"
#include "FPSCharacter.h"

// 기본값을 설정합니다.
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);

    // "look" 바인딩을 구성합니다.
    PlayerInputComponent->BindAxis("Turn", this, &AFPSCharacter::AddControllerYawInput);
    PlayerInputComponent->BindAxis("LookUp", this, &AFPSCharacter::AddControllerPitchInput);

    // "action" 바인딩을 구성합니다.
    PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &AFPSCharacter::StartJump);
    PlayerInputComponent->BindAction("Jump", IE_Released, this, &AFPSCharacter::StopJump);
}

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);
}

void AFPSCharacter::OnStartJump()
{
    bPressedJump = true;
}

void AFPSCharacter::OnStopJump()
{
    bPressedJump = false;
}

원본

캐릭터 점프 구현 튜토리얼을 내가 보기 좋게 정리한 것입니다.
처음 이시라면 축 매핑 튜토리얼을 보고 축 매핑이 선행 되어야 합니다

좋은 웹페이지 즐겨찾기