[UE5] TIL - 16 <GetHitResultUnderCursor, DrawDebugSphere>

ChangJin·2024년 4월 3일
0

Unreal Engine5

목록 보기
43/102
post-thumbnail

2024-04-03

깃허브!
https://github.com/ChangJin-Lee/ARproject
https://github.com/ChangJin-Lee/ToonTank

느낀점
GetHitResultUnderCursor로 화면에 보이는 물체에 대한 카메라 커서의 충돌된 위치를 반환하는 방법을 배우게 되었다. 캐릭터의 회전 방향을 정할때 유용하게 쓰인다고한다. CriptRaider때 배웠던 DrawDebugSphere를 또 사용했다. 디버그 구체를 그려서 현재 마우스가어떤 위치에 있는지를 알 수 있다.

TIL

  • GetHitResultUnderCursor
  • DrawDebugSphere

GetHitResultUnderCursor

https://dev.epicgames.com/documentation/en-us/unreal-engine/API/Runtime/Engine/GameFramework/APlayerController/GetHitResultUnderCursor?application_version=5.3


  • 월드에 Start라는 FVector 타입의 점이 있고 End라는 FVector 타입의 점이 있다. 두 점을 잇는 직선을 그리는 행위를 Line Trace라고 부른다.

  • 그 직선 사이에 물체가 있다면 Hit Event가 발생하고 이걸 Trace Hit이라고 부른다.

  • 이러한 Trace Hit의 결과는 FHitResult에 담겨지는데 여기에는 Hit Location, Hit Actor가 있다.

  • 이전에 만들어놓은 APlayerController 변수를 사용하면 된다.

// PlayerControllerRef가 유효한지 확인해야한다.
// 포인터는 항상 null 포인터인지 아닌지 확인해야한다.
// ECollisionChannel은 열겨형 타입이다.
// bTraceComplex를 false로 했고
// HitResult에 충돌 결과를 저장했다.
	if(PlayerControllerRef)
	{
		FHitResult HitResult;
		PlayerControllerRef->GetHitResultUnderCursor(
			ECollisionChannel::ECC_Visibility,
			false,
			HitResult);
	}

  • Collision Channel을 통해서 Trace를 사용하게 된다.
  • Visibility Channel에서 어떤 물체가 막히고 막히지 않는지를 제어할 수 있다.


DrawDebugSphere

https://docs.unrealengine.com/4.27/en-US/API/Runtime/Engine/DrawDebugSphere/


  • 공간에 있는 위치를 알려주는 좋은 방법이다.
  • 실제 게임에서는 쓰이지 않고 debugging에 쓰인다.


  • 매 프레임마다 마우스의 위치를 반환하기 위해서 Tick 함수안에 다음과 같이 만들었다.
  • 주석 처리되어 있는 부분을 활성화하면 탱크의 머리 위에 debugSphere가 따라다닌다.
// Called every frame
void ATank::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	if(PlayerControllerRef)
	{
		FHitResult HitResult;
		PlayerControllerRef->GetHitResultUnderCursor(
			ECollisionChannel::ECC_Visibility,
			false,
			HitResult);
		
		// DrawDebugSphere(
		// 	GetWorld(),
		// 	GetActorLocation() + FVector(0.0f,0.0f,100.0f),
		// 	100.f,
		// 	24,
		// 	FColor::Red,
		// 	false,
		// 	-1);

		DrawDebugSphere(
			GetWorld(),
			HitResult.ImpactPoint,
			50.f,
			24,
			FColor::Red,
			false,
			-1.f);
	}
}
profile
Unreal Engine 클라이언트 개발자

0개의 댓글