포스트

UE5 C++ | Crypt Raider, 씬 컴포넌트와 라인트레이싱 준비 (유데미)

언리얼 강의 중 Section 4를 정리한 네번째 글입니다.

UE5 C++ | Crypt Raider, 씬 컴포넌트와 라인트레이싱 준비 (유데미)

강의 정보 🎓


링커, 헤더, 인클루드

전체 컴파일 흐름:

  • 소스 코드 → (언리얼 헤더 툴) → 전처리 → 컴파일러 → 링커 → 실행 파일

Compilation Steps.png

Compilation Result.png


FMath::VInterpConstantTo 📦

  • UMover 클래스에 다음 멤버 변수를 추가한다.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
      private:
      	UPROPERTY(EditAnyWhere)
      	FVector MoveOffset;
      	UPROPERTY(EditAnyWhere)
      	float MoveTime = 4.0f;
      	UPROPERTY(EditAnyWhere)
      	bool ShouldMove = false;
        	
      	FVector OriginalLocation;
    
  • BeginPlay()TickComponent()에서 위치 이동을 구현한다.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    
      void UMover::BeginPlay(){
      	...
      	OriginalLocation = GetOwner()->GetActorLocation();
      }
        
      void UMover::TickComponent(...){
      	...
      	if(ShouldMove)
      	{
      		FVector CurrentLocation = GetOwner()->GetActorLocation();
      		FVector TargetLoaction = OriginalLocation + MoveOffset;
      		float Speed = FVector::Distance(OriginalLocation, TargetLoaction) / MoveTime;
        
      		FVector NewLocation = FMath::VInterpConstantTo(CurrentLocation, TargetLoaction, DeltaTime, Speed);
      		GetOwner()->SetActorLocation(NewLocation);
      	}
      }
    
  • Static Mesh의 Mobility는 반드시 Movable로 설정이 필요하다.
  • Mover에서 Move Offset과 Move Tiem, Should Move를 수정해주면 이동하는 것을 볼 수 있다.

씬 컴포넌트 🎮

  • New C++ ClassSceneComponent 선택 → Grabber생성한다.
  • BP_Player의 Camera Component 하위로 Grabber 컴포넌트를 추가한다.

    Grabber Component.png

  • 컴포넌트의 회전을 출력한다.

    1
    2
    3
    4
    5
    
      void UGrabber::TickComponent(...){
      	...
      	FRotator Rotator = GetComponentRotation();
      	UE_LOG(LogTemp, Display, TEXT("Rotator: %s"), *Rotator.ToCompactString());
      }
    

라인 트레이스 & 스위프 🔦

트레이스 채널 설정

  • Project Settings → Engine → Collision → New Trace Channel
    • 이름: Grabber
    • 기본 반응: Ignore
  • 에디터를 저장 후 다시 프로젝트를 실행하면 다음처럼 Grabber가 생긴 것을 볼 수 있다.

    Grabber Trace Channel.png


GetWorld() 🌍

  • 월드는 레벨을 여러 개 가질 수 있는 오브젝트 타입이다.
  • 맵 또는 샌드박스를 표현하는 최상위 레벨 오브젝트이다.
  • Line Trace 와 Sweep을 하기 위해 필요하다.

UWorld.png

  • 월드 시간 확인:

    1
    2
    3
    4
    5
    6
    7
    
      #include "Engine/World.h"
      void UGrabber::TickComponent(...){
      	...
      	UWorld* World = GetWorld();
      	double TimeSeconds = World->TimeSeconds;
      	UE_LOG(LogTemp, Display, TEXT("World Time Seconds: %f"), TimeSeconds);
      }
    

DrawDebugLine() 🧪

  • Grabber 헤더에 MaxGrabDistance 멤버 변수를 추가한다.
  • DrawDebugLine으로 라인 트레이스를 시각화한다.
1
2
3
4
5
6
7
#include "Engine/World.h"
void UGrabber::TickComponent(...){
	...
	FVector Start = GetComponentLocation();
	FVector End = Start + GetForwardVector() * MaxGrabDistance;
	DrawDebugLine(GetWorld(), Start, End, FColor::Red, false);
}

플레이 후 자유 시점에서 보게 되면 다음처럼 빨간 선이 보인다. 플레이 후 자유 시점에서 보게 되면 다음처럼 빨간 선이 보인다.


요약 및 느낀점 📝

  • 씬 컴포넌트를 추가하여 플레이어 카메라의 회전 정보 등을 가져와서 라인 트레이스를 위한 디버깅용 빨간 선까지 구현하였다.
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.