입력 매핑
프로젝트 세팅 -> 엔진-입력 -> 바인딩
C++ 매핑
// Called every frame
void ACritter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
FVector NewLocation = GetActorLocation() + (CurrentVelocity * DeltaTime);
SetActorLocation(NewLocation);
}
// Called to bind functionality to input
void ACritter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis(TEXT("MoveForward"), this, &ACritter::MoveForward);
PlayerInputComponent->BindAxis(TEXT("MoveRight"), this, &ACritter::MoveRight);
}
void ACritter::MoveForward(float Value)
{
CurrentVelocity.X = FMath::Clamp(Value, -1.f, 1.f) * MaxSpeed;
}
void ACritter::MoveRight(float Value)
{
CurrentVelocity.Y = FMath::Clamp(Value, -1.f, 1.f) * MaxSpeed;
}
이제 키보드로 컨트롤이 되는지 확인
전체코드
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "Critter.generated.h"
UCLASS()
class BASIC_API ACritter : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
ACritter();
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;
UPROPERTY(EditAnywhere, Category = "Mesh")
class UStaticMeshComponent* MeshComponent;
UPROPERTY(EditAnywhere)
class UCameraComponent* Camera;
FVector CurrentVelocity;
float MaxSpeed;
private:
void MoveForward(float Value);
void MoveRight(float Value);
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "Critter.h"
#include <Engine/Classes/Components/StaticMeshComponent.h>
#include <Engine/Classes/Components/InputComponent.h>
#include <Engine/Classes/Camera/CameraComponent.h>
// Sets default values
ACritter::ACritter()
{
// 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;
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
// Mesh 지정
MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComponent"));
MeshComponent->SetupAttachment(GetRootComponent());
// Camera 지정
Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
Camera->SetupAttachment(GetRootComponent());
Camera->SetRelativeLocation(FVector(-300.0f, 0.f, 300.f));
Camera->SetRelativeRotation(FRotator(-45.f, 0.f, 0.f));
AutoPossessPlayer = EAutoReceiveInput::Player0;
CurrentVelocity = FVector(0.f);
MaxSpeed = 100.f;
}
// Called when the game starts or when spawned
void ACritter::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ACritter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
FVector NewLocation = GetActorLocation() + (CurrentVelocity * DeltaTime);
SetActorLocation(NewLocation);
}
// Called to bind functionality to input
void ACritter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis(TEXT("MoveForward"), this, &ACritter::MoveForward);
PlayerInputComponent->BindAxis(TEXT("MoveRight"), this, &ACritter::MoveRight);
}
void ACritter::MoveForward(float Value)
{
CurrentVelocity.X = FMath::Clamp(Value, -1.f, 1.f) * MaxSpeed;
}
void ACritter::MoveRight(float Value)
{
CurrentVelocity.Y = FMath::Clamp(Value, -1.f, 1.f) * MaxSpeed;
}