언리얼 스크린 중앙

언리얼 스크린 중앙.

총을 구현하기 위해 에임을 설정할 때 스크린 중앙을 가져오는 방법은 여러가지가 있습니다.

이 글에서는 간단한 방법 두가지만 작성하였습니다.

APlayerCameraManager를 사용하는 방법과 UCameraComponent를 사용한 방법입니다.

APlayerCameraManager.

//#include "Camera/PlayerCameraManager.h" 필요
APlayerCameraManager* CameraManager = GetWorld()->GetFirstPlayerController()->PlayerCameraManager;
FVector Loc = CameraManager->GetCameraLocation();
FVector Rot = CameraManager->GetActorForwardVector();
//FVector Rot = CameraManager->GetCameraRotation().Vector();

DrawDebugLine(GetWorld(), Loc, (Rot * 1000) + Loc, FColor::Red, false, 10);

UCameraComponent를 사용한 방법.

//Header
UPROPERTY(EditDefaultsOnly, Category = "Camera")
TObjectPtr<USpringArmComponent> SprintArmComponent;
UPROPERTY(EditDefaultsOnly, Category = "Camera")
TObjectPtr<UCameraComponent> ViewCamera;

/*Cpp constructor
*그냥 작성하던 제 코드중 일부 긁어서 복붙해놓았습니다.
*/
SprintArmComponent = CreateDefaultSubobject<USpringArmComponent>(TEXT("SprintArm"));
SprintArmComponent->bUsePawnControlRotation = true;
SprintArmComponent->SetupAttachment(GetMesh(),FName("Head_Camera"));
SprintArmComponent->TargetArmLength = 0.f;
SprintArmComponent->bEnableCameraRotationLag = true;
SprintArmComponent->CameraRotationLagSpeed = 90.f;

ViewCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("ViewCamera"));
ViewCamera->SetupAttachment(SprintArmComponent);

//Other Func
FVector ViewLoc = ViewCamera->GetComponentLocation();
FVector ViewRot = ViewCamera->GetForwardVector();
//FVector ViewRot = ViewCamera->GetComponentRotation().Vector();

DrawDebugLine(GetWorld(), ViewLoc, (ViewRot * 1000) + ViewLoc, FColor::Red, false, 10);

결과.

카메라의 중심에서 앞으로 선을 그리는 예제라서 움직이지 않으면 잘 보이지 않습니다.

그래서 DrawDebugLine의 시간을 길게 설정하였고 아래 사진에서는 위의 코드보다 더 두껍게 설정하였습니다.

처음 서있는 위치.

앞.

발사.

 

Designed by JB FACTORY