본문 바로가기
카테고리 없음

[UE 5 C++]플레이어 input 두 축으로 움직이기

by the_cat_Soy 2023. 11. 26.

cpp

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


#include "Myplayer.h"

// Sets default values
AMyplayer::AMyplayer()
{
 	// 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;

	MeshComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComp"));

}

// Called when the game starts or when spawned
void AMyplayer::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void AMyplayer::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	//사용자의 입력에 따라 상하 좌우로 움직이고 싶다
	//궁극적으로 하고 싶은 것이 첫번째
	//1.입력이 필요하다
	//2.방향이 필요하다
	//3.이동이 필요하다
	//P = P0 +vt

	FVector P0 = GetActorLocation();
	FVector vt = Direction * Speed * DeltaTime;
	FVector P = P0 + vt;

	SetActorLocation(P);

	Direction = FVector::Zero();


}

// Called to bind functionality to input
void AMyplayer::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);
	PlayerInputComponent->BindAxis(TEXT("Horizontal"), this, &AMyplayer::Horizontal);
	PlayerInputComponent->BindAxis(TEXT("Vertical"), this, &AMyplayer::Vertical);
}

void AMyplayer::Horizontal(float Value)
{

	Direction.Y = Value;
	
}

void AMyplayer::Vertical(float Value)
{
	Direction.X = Value;
}

 

 

Header

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

#pragma once

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

UCLASS()
class STUDY_API AMyplayer : public APawn
{
	GENERATED_BODY()

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

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;

public:

	UPROPERTY(VisibleAnywhere)
	class UStaticMeshComponent* MeshComp;


	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Move")
	FVector Direction;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Move")
	float Speed = 500;


	void Horizontal(float Value);
	
	void Vertical(float Value);


};