카테고리 없음

[UE 5 C++] 하나의 input으로 여러개의 Pawn 움직이기

the_cat_Soy 2023. 11. 26. 20:11

1. 코드 작성하기

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")
	class AMyplayer* A;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Move")
	class AMyplayer* B;

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

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


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


};

 

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;
	A->Direction.Y = Value;
	B->Direction.Y = Value;
}

void AMyplayer::Vertical(float Value)
{
	Direction.X = Value;
	A->Direction.X = Value;
	B->Direction.X = Value;
}

 

 

2. 에디터로 돌아와서 BP 폰을 월드에 배치한다

 

3.카메라 액터 배치후 Activate

 

4.Auto  Posses Player 0으로 셋팅

- Auto Posses Player 0 셋팅 후 액터를 복제하게 되면 통제권이 대부분 마지막 액터로 넘어간다.

-모든 액터가 같은 설정을 하지 않도록 주의! 한 액터에만 빙의되도록 설정

-함께 움직일 액터들을 연결한다