Unreal Engine/이론

[Unreal Engine] 5) Input

GiveZero 2023. 1. 24. 15:42

Edit - Project Setting - Input 에 다음과 같이 선언후 진행

 

void AMyPawn::MoveForward(float AxisValue)
{
	currentVelocity.X = FMath::Clamp(AxisValue, -1.f, 1.f) * 100.f;
}

void AMyPawn::MoveRight(float AxisValue)
{
	currentVelocity.Y = FMath::Clamp(AxisValue, -1.f, 1.f) * 100.f;
}

void AMyPawn::StartScaleUp()
{
	bScale = true;
}

void AMyPawn::StopScaleUp()
{
	bScale = false;
}

먼저, 앞뒤양옆 이동과 크기 조절을 할수있도록 설정

// Called to bind functionality to input
void AMyPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	InputComponent->BindAxis("MoveForward", this, &AMyPawn::MoveForward);
	InputComponent->BindAxis("MoveRight", this, &AMyPawn::MoveRight);
	InputComponent->BindAction("ScaleUp", EInputEvent::IE_Pressed, this, &AMyPawn::StartScaleUp);
	InputComponent->BindAction("ScaleUp", EInputEvent::IE_Released, this, &AMyPawn::StopScaleUp);
}

이후, Binding 작업을 통해 Input 설정과 같이 만듦.

 

AMyPawn::AMyPawn()
{
	// 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"));
	OurVisibleComponent = CreateDefaultSubobject<USceneComponent>(TEXT("VisbleComponent"));
	OurVisibleComponent->SetupAttachment(RootComponent);
}

RootComponent , OurVisibleComponent를 USceneComponent 컴포넌트로 설정후,

RootComponent (부모 컴포넌트) 와 OurVisibleComponent(자식 컴포넌트)로 설정 

* USceneComponent는 Transform정보만을 지닌 가벼운 컴포넌트

void AMyPawn::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	{
		float currentScale = OurVisibleComponent->GetComponentScale().X;
		if (bScale)
		{
			currentScale += DeltaTime;
		}
		else
		{
			currentScale -= (DeltaTime * 0.5f);
		}
		currentScale = FMath::Clamp(currentScale, 1.0f, 2.0f);
		OurVisibleComponent->SetWorldScale3D(FVector(currentScale));
	}

	{
		if (!currentVelocity.IsZero())
		{
			FVector NewLocation = GetActorLocation() + (-currentVelocity * DeltaTime);
			SetActorLocation(NewLocation);
		}
	}

}

이후, 이동과 크기조절을 할 수 있는 Tick 구현