UE4 Character move forward & right

주홍영·2022년 3월 25일
0

UE4

목록 보기
2/5

Character를 input을 통해 움직이기 위해
move forward와 right를 구현한다

먼저 Main.h에 function을 forward declaration 한다

	/** Called for forwards/backwards input */
	void MoveForward(float Value);

	/** Called for side to side input */
	void MoveRight(float Value);

여기서 Value는 키보드 WASD를 통해서 받아올 Value를 의미한다

그리고 Main.cpp에 function definition을 작성한다

void AMain::MoveForward(float Value)
{
	if ( (Controller != nullptr) && (Value != 0.0f) )
	{
		// find out which way is forward
		const FRotator Rotation = Controller->GetControlRotation();
		const FRotator YawRoatation(0.f, Rotation.Yaw, 0.f);

		const FVector Direction = FRotationMatrix(YawRoatation).GetUnitAxis(EAxis::X);
		AddMovementInput(Direction, Value);
	}
}


void AMain::MoveRight(float Value)
{
	if ((Controller != nullptr) && (Value != 0.0f))
	{
		// find out which way is forward
		const FRotator Rotation = Controller->GetControlRotation();
		const FRotator YawRoatation(0.f, Rotation.Yaw, 0.f);

		const FVector Direction = FRotationMatrix(YawRoatation).GetUnitAxis(EAxis::Y);
		AddMovementInput(Direction, Value);
	}
}

Controller는 ACharacter class에 정의되어 있고 이를 상속 받았기 때문에 사용할 수 있다
Controller의 Rotation을 받아와 움직일 방향을 정하기 위해 위와 같이 코드를 작성한다

profile
청룡동거주민

0개의 댓글