Line trace randomly does not return a hit (even when it clearly hit something)

Not sure if this is a glitch in the engine or my code is wrong.

Here is a video demonstrating the problem. When the line trace does not hit anything, it draws a red line and prints “Not Hit” in the top left corner of the screen. As you can see from the video, the line trace is clearly hitting the wall but at certain spots, the engine returns no hit and draws a red line instead.

Why is this happening?

Here is the code. The game is a third person shooter so I’m using the doing 2 line traces. One from camera to calculate tracer end point, then another one from the character’s head to the tracer end point.



    AActor* MyOwner = GetOwner(); // MyOwner is the player pawn
    if (MyOwner) {
        // Get start point and end point of first line trace, using third person camera
        FVector CameraLocation;
        FRotator CameraRotation;
        TArray<UCameraComponent*> CameraComps;
        MyOwner->GetComponents<UCameraComponent>(CameraComps);
        UCameraComponent* MyOwnerCamera = CameraComps[0];
        CameraLocation = MyOwnerCamera->GetComponentLocation();
        CameraRotation = MyOwnerCamera->GetComponentRotation();
        FVector ShotDirection = CameraRotation.Vector();
        FVector TraceEnd = CameraLocation + (ShotDirection * 10000);

        // Set up QueryParams to ignore hitting the player pawn and weapon (this = weapon)
        FCollisionQueryParams QueryParams;
        QueryParams.AddIgnoredActor(MyOwner);
        QueryParams.AddIgnoredActor(this);

        FHitResult CameraHit;
        if (GetWorld()->LineTraceSingleByChannel(CameraHit, CameraLocation, TraceEnd, COLLISION_WEAPON, QueryParams)) {

            // If line trace from camera hit something, create another line trace from player pawn's face to the impact point of camera line trace
            FVector EyeLocation;
            FRotator EyeRotation;
            MyOwner->GetActorEyesViewPoint(EyeLocation, EyeRotation);
            FHitResult ActualHit;

            if (GetWorld()->LineTraceSingleByChannel(ActualHit, EyeLocation, CameraHit.ImpactPoint, COLLISION_WEAPON, QueryParams)) {
                // If line trace from player's face hit something, draw a green debug line and print "Hit"
                DrawDebugLine(GetWorld(), EyeLocation, CameraHit.Location, FColor::Green, false, 10.f, 0, 1.f);
                GEngine->AddOnScreenDebugMessage(-1, 3.f, FColor::Yellow, "Hit");
            } else {
                // If line trace from player's face does not record a hit, draw a red debug line and print "Not Hit"
                DrawDebugLine(GetWorld(), EyeLocation, CameraHit.Location, FColor::Red, false, 10.f, 0, 1.f);
                GEngine->AddOnScreenDebugMessage(-1, 3.f, FColor::Yellow, "Not Hit");
            }
        }
    }


Well, to start with, your first line trace going out of camera location to the trace end is actually always hitting something. I would first check and compare values of EyeLocation and CameraHit.ImpactPoint if they seem to be fine. If they are, then I’d try to use UKismetSystemLibrary::LineTraceSingle function since it provides an option to draw debug traces so you will be able to verify accurately whats going on. If none of that helps I’ll try to come up with some other ideas :smiley: