How to make line trace hit collision part of mesh?

I want a line trace to hit the collision part of a mesh. For example, I want it to hit what is shown when you’re inspecting a mesh and choose ‘Show’ → ‘Simple Collision’. I can only get it to hit the visible part of the mesh.
For example, If I import a mesh with a hole in it, I want the line trace to return a hit even if I’m looking through the hole.
For another example, If I import a mesh with a custom collision mesh (ucx/ubx/etc.), I want to hit the collision mesh and not the visible mesh.
How can I do this?

The line trace takes “FCollisionQueryParams” which is where you set “bTraceComplex == false”.

Code snippet of our cpp trace code in a “walking” player state which goes straight through the center of the screen.

// get current camera location and rotation
FVector cameraLocation = _playerController->PlayerCameraManager->GetCameraLocation();
FRotator cameraRotation = _playerController->PlayerCameraManager->GetCameraRotation();
FVector cameraDirection = cameraRotation.Vector().GetSafeNormal();

// calculate ray start point, ray end point, hit result, collision parameters
// ray start point is the camera location
// ray end point is a ray cast through the middle of the screen
FVector traceStartLocation = cameraLocation;
FVector traceEndLocation = cameraLocation + MAXIMUM_INTERACTION_DISTANCE * cameraDirection;
FHitResult traceHitResult(ForceInit);

// simple collision only
FCollisionQueryParams rayTraceParameters(FName("Trace"), true);
rayTraceParameters.bTraceComplex = false;

// do ray trace
_playerController->GetWorld()->LineTraceSingleByChannel(traceHitResult,										traceStartLocation, traceEndLocation, ECC_Pawn, traceParameters);

// get unreal trace actor, trace component, and trace vector
_traceActor = traceHitResult.GetActor();
_traceComponent = traceHitResult.GetComponent();
FVector traceVector = traceHitResult.Location - traceStartLocation;

1 Like

Thanks! I was using FCollisionQueryParams::DefaultQueryParam. I didn’t realize the default query params were setting bTraceComplex to true.

1 Like