스토리지

Pawn 움직이기 본문

Unreal Engine/0. Unreal 5

Pawn 움직이기

ljw4104 2023. 7. 12. 17:49

1. 입력 설정

  • Project Settings > Input > Axis Mappings

  • 뒤로 가는 것은 앞으로 가는 것의 반대이기 때문에 -1값을 넣어줌

 

2. 입력값 받기

  • 입력값은 Pawn 만들 때 추가된 SetupPlayerInputComponent 함수를 통해서 설정할 수 있다.
void ABird::MoveForward(float Value)
{
	if (Controller && Value != 0.f)
	{
		FVector Forward = GetActorForwardVector();
		AddMovementInput(Forward, Value);
	}
}

void ABird::Turn(float Value)
{
	AddControllerYawInput(Value);
}

void ABird::LookUp(float Value)
{
	AddControllerPitchInput(Value);
}

// Called to bind functionality to input
void ABird::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);
	
	PlayerInputComponent->BindAxis(FName("MoveForward"),this, &ABird::MoveForward);
	PlayerInputComponent->BindAxis(FName("Turn"), this, &ABird::Turn);
	PlayerInputComponent->BindAxis(FName("LookUp"), this, &ABird::LookUp);
}
BindAxis(FName(축이름), UserClass 포인터, 대응되는 함수주소)
  • 해당코드 추가 후 블루프린트에서 FloatingPawnMovement 컴포넌트를 추가해주어야 한다.
  • 각도 변경을 위해 AddControllerYawInput와 AddControllerPitchInput을 사용했기 때문에 블루프린트에서 해당 옵션을 체크해준다.

튜토리얼이라서 축 이름에 그냥 넣은거같은데 나중에 상수만 모아놓은 헤더 하나 따로 만들고 거기에 축 이름등 값들 다 넣고 그걸로 바꿔야할듯.

 

====================================================================

축 이름 변수로 다 뺌

#pragma once

class FConsts
{
public:
	inline static FName MoveForward = TEXT("MoveForward");
	inline static FName Turn = TEXT("Turn");
	inline static FName LookUp = TEXT("LookUp");
};
// Called to bind functionality to input
void ABird::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);
	
	PlayerInputComponent->BindAxis(FConsts::MoveForward,this, &ABird::MoveForward);
	PlayerInputComponent->BindAxis(FConsts::Turn, this, &ABird::Turn);
	PlayerInputComponent->BindAxis(FConsts::LookUp, this, &ABird::LookUp);
}

'Unreal Engine > 0. Unreal 5' 카테고리의 다른 글

Character  (0) 2023.07.13
카메라 추가  (0) 2023.07.12
Forward Declaration (전방 선언)  (0) 2023.07.12
Pawn 추가  (0) 2023.07.12
Component  (0) 2023.07.11
Comments