RealDocs

UWorld::LineTraceSingleByChannel

function Engine Since 4.0
#include "Engine/World.h"
Access: public Specifiers: const

Description

Traces a ray from Start to End using the specified collision channel and returns the first blocking hit. Returns true if a blocking hit was found.

Signature

bool LineTraceSingleByChannel( struct FHitResult& OutHit, const FVector& Start, const FVector& End, ECollisionChannel TraceChannel, const FCollisionQueryParams& Params = FCollisionQueryParams::DefaultQueryParam, const FCollisionResponseParams& ResponseParam = FCollisionResponseParams::DefaultResponseParam ) const

Parameters

Name Type Description Default
OutHit struct FHitResult& Receives the first blocking hit. Only valid when the function returns true.
Start const FVector& World-space start point of the ray.
End const FVector& World-space end point of the ray.
TraceChannel ECollisionChannel Collision channel that determines which components are considered blocking hits.
Params const FCollisionQueryParams& Additional query options: ignored actors, complex collision, draw debug, etc. FCollisionQueryParams::DefaultQueryParam
ResponseParam const FCollisionResponseParams& Override collision response settings for this trace. FCollisionResponseParams::DefaultResponseParam

Return Type

bool

Caveats & Gotchas

  • The owning actor is not automatically excluded — call Params.AddIgnoredActor(this) to prevent the trace from hitting the actor performing it.
  • OutHit data is only meaningful when the function returns true. OutHit.GetActor() will return null on a miss.
  • To capture all overlapping hits along the ray (not just the first blocker), use LineTraceMultiByChannel instead.

Example

Raycast forward from the camera C++
FHitResult Hit;
FVector Start = Camera->GetComponentLocation();
FVector End   = Start + Camera->GetForwardVector() * 5000.f;

FCollisionQueryParams Params;
Params.AddIgnoredActor(this);

if (GetWorld()->LineTraceSingleByChannel(Hit, Start, End, ECC_Visibility, Params))
{
	AActor* HitActor = Hit.GetActor();
}

Version History

Introduced in: 4.0

Version Status Notes
5.6 stable

Feedback

Was this helpful?

Suggest an edit

Select a field above to begin editing.