본문 바로가기

프로그래밍/Unreal

[UE4] Line Trace


UWorld 객체의 LineTraceSingle() 메쏘드를 사용해 얻을 수 있다.

결과는 FHitResult 구조체로 얻을 수 있으며, 해당 구조체에 액터나 거리 위치등 세부 정보가 포함된다.



https://wiki.unrealengine.com/Trace_Functions



static FORCEINLINE bool Trace(

UWorld* World,

AActor* ActorToIgnore,

const FVector& Start,

const FVector& End,

FHitResult& HitOut,

ECollisionChannel CollisionChannel = ECC_Pawn,

bool ReturnPhysMat = false)

{


FCollisionQueryParams TraceParams = FCollisionQueryParams( FName(TEXT("MY_TRACE")), true, ActorToIgnore);


TraceParams.bTraceComplex = true;

TraceParams.bReturnPhysicalMaterial = ReturnPhysMat;


TraceParams.AddIgnoredActor(ActorToIgnore);



HitOut = FHitResult(ForceInit);


World->LineTraceSingle(

HitOut,

Start,

End,

CollisionChannel,

TraceParams

);


return (HitOut.GetActor() != NULL );

}



위와 같은 검사 함수를 만들고 아래처럼 사용하면 된다.


// 시작 위치

const FVector Start = GetFocalLocation();


// 끝위치

const FVector End = Start + GetControlRotation().Vector() * 256;


FHitResult HitData(ForceInit);


if( UMyFunction::Trace( GetWorld(), GetPawn(), Start, End, HitData) )

{

if(HitData.GetActor())

{


}

}





블루프린트 LineTraceByChannel 

캐릭터가 바라보고 있는 오브젝트를 알아낼때 필요.


예제 ) Spot Light 색상 바꾸기


A클래스에 Custom Event , 입력값 Linear Color 추가

이벤트가 호출되면 Spot Light 컴포넌트에 Set Light Color 로 색상 변경


캐릭터 클래스

특정 이벤트(키입력, 마우스 입력 등) 발생시 Line Trace

LineTraceByChannel

입력

Start : 캐릭터에 포함된 카메라 콤포넌트 GetWorldLocation

End : 카메라 콤포넌트의 전방 노말벡터 GetForwardVector * float + Start 위치


출력

Return Value 로 branch

Out Hit : Break Hit Result 


Break Hit Result

Hit Actor : A클래스로 형변환


형변환 성공하면 커스텀 이벤트 호출




'프로그래밍 > Unreal' 카테고리의 다른 글

[UE4] 폰트  (0) 2017.09.26
[UE4] C++ 에셋, 오브젝트 레퍼런싱/객체생성  (1) 2017.09.25
[UE4] CSV 파일  (0) 2017.09.24
[UE4] unreal delegate  (0) 2017.09.24
[UE4] 마우스 입력  (0) 2017.09.17
[UE4] 아웃라인 : 스텐실  (0) 2017.09.16
[UE4] 메쉬 외곽선 효과  (0) 2017.09.15
[UE4] 언리얼 기본 예제 분석  (0) 2017.09.13
[UE4] 액터  (0) 2017.09.12
[UE4] C++ 기본 사항 노트  (0) 2017.09.06