UE5/이동, 회전

[UE5] 카메라달기, 우클릭시 이동-회전 구현

Mini_96 2024. 5. 25. 16:13

1. 입력 매핑

1. 인풋데이터에 변수추가

TObjectPtr<UInputAction> RightClickThenMoveThere;

 

2. 인풋액션 추가

 

3. IMC에 매핑, (구현)

 

2. Character.cpp 카메라 달아주기

bp에서 했던 과정을 코드로 써준다고 생각하면 쉽다.

BP의 +Add버튼이 cpp 에서는 : CreateDefaultSubobject<타입>(TEXT(변수명)) 임을 기억하자.

class USpringArmComponent;
class UCameraComponent;

UENUM(BlueprintType)
enum EViewMode
{
@@ -31,6 +34,15 @@ class AURA_API ARangerCharacter : public AAuraCharacterBase
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

	//카메라는 캐릭터만의 속성임 -> 여기서구현
	UPROPERTY(EditAnywhere,Category="Input")
	TObjectPtr<USpringArmComponent> SpringArmComponent;

	//카메라는 캐릭터만의 속성임 -> 여기서구현
	UPROPERTY(EditAnywhere,Category="Input")
	TObjectPtr<UCameraComponent> CameraComponent;
SpringArmComponent = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComponent"));
	SpringArmComponent->SetupAttachment(RootComponent);
	//카메라를 springarm에 넣기
	CameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComponent"));
	CameraComponent->SetupAttachment(SpringArmComponent);

	SpringArmComponent->TargetArmLength = 800.f;
	SpringArmComponent->SetRelativeRotation(FRotator(-45.f, 0.f, 0.f));

 

3. 우클릭시 이동, 회전 구현

1. 컨트롤러 > 바인딩해줌

EnhancedInputComponent->BindAction(InputActions->RightClickThenMoveThere, ETriggerEvent::Triggered, this, &AAuraPlayerController::RightClickMove);

2. a에서 b로가는 방향벡터는 b-a 임을 기억!

* gethitresultundercursorbychannel => 마우스 찍은 위치를 기록

* b-a 벡터를 

Pawn(Player)을 얻은후 addMove(b-a) 해준다.

void AAuraPlayerController::RightClickMove(const FInputActionValue& InputActionValue)
{
	FHitResult Hit;
	this->GetHitResultUnderCursorByChannel(UEngineTypes::ConvertToTraceType(ECC_Visibility),true,Hit);
	APawn* ControlledPawn = GetPawn<APawn>();
	if(nullptr==ControlledPawn) return ;

	if(Hit.bBlockingHit)
	{
		//To - From == From에서 To로 가는 방향벡터!
		FVector LookAtTarget = Hit.ImpactPoint;
		FVector ToTarget = LookAtTarget - ControlledPawn->GetActorLocation();
		ControlledPawn->AddMovementInput(ToTarget);

	}

}

 

4. 플레이어가 컨트롤러를 따를것인지 (no)

카메라가 플레이어를 따를것인지 (no)

부드럽게회전할것인지 (yes)

캐릭 이동방향으로 자동회전할것인지 설정!  (yes) //이부분때문에 2시간동안 회전이 안되는 버그가 있었다..

	// 컨트롤러 기반 회전을 비활성화합니다 (Pitch, Roll, Yaw 축).
	bUseControllerRotationPitch = false;
	bUseControllerRotationYaw = false; //캐릭터는 컨트롤러의 요를 안따라간다.
	bUseControllerRotationRoll = false;



	SpringArmComponent->bUsePawnControlRotation = false;

	SpringArmComponent->bInheritPitch = false;
	SpringArmComponent->bInheritYaw = false; //카메라는 플레이어의 요를 안따른다.
	SpringArmComponent->bInheritRoll = false;

	SpringArmComponent->bDoCollisionTest = false;


	GetCharacterMovement()->RotationRate = FRotator(0.f, 360.f, 0.f);
	GetCharacterMovement()->bOrientRotationToMovement = false;
	GetCharacterMovement()->bUseControllerDesiredRotation = true; //의지대로 부드럽게 회전함.

	GetCharacterMovement()->bOrientRotationToMovement=true; // 캐릭방향으로 자동회전!