"Get Hit Result Under Cursor for Objects" face index always -1

I’m having a problem with line trace. If I use “Get Hit Result Under Cursor for Objects” the face index is not working, it always returns -1. If I use “Line Trace by Channel” tracing the same spot, same object, I get a proper face index, example 7.

It’s pretty easy to reproduce on any sample level. Do you know why the face index is not working?

Side note, I tried to investigate the C++ code behind that BP.

Here is the stack of calls:

LineTraceSingleByObjectType(...,FCollisionQueryParams(SCENE_QUERY_STAT(ClickableTrace), bTraceComplex),...)

As you see in the last line he is passing some hardcoded parameters, then later in the code it seems to check for a FCollisionQueryParams.bReturnFaceIndex

At the moment I decided to do it differntly, and here is what I use and works ok. Still my question above stands, on why it returns -1 and there is no way to change it.

Hi Talad,

I recently encountered the same issue and found that there’s a boolean “bReturnFaceIndex” in FCollisionQueryParams. For the normal LineTrace function you can pass in a custom FCollisionQueryParams, where you can set bReturnFaceIndex to true. However, GetHitResultUnderCursor() doesn’t allow you to pass in custom collision params. I basically dug through the engine code and rewrite the function implementation to support passing in collision params. Hope this might help someone.

#include "Engine/LocalPlayer.h"
#include "Runtime/Engine/Classes/GameFramework/PlayerController.h"

bool GetHitResultUnderCursor(ECollisionChannel TraceChannel, FCollisionQueryParams CollisionQueryParams, FHitResult & HitResult)
    APlayerController* PlayerController= Cast<APlayerController>(Pawn->GetController());
    ULocalPlayer* LocalPlayer = Cast<ULocalPlayer>(PlayerController->Player);
    bool bHit = false;
    if (LocalPlayer && LocalPlayer->ViewportClient)
        FVector2D MousePosition;
        if (LocalPlayer->ViewportClient->GetMousePosition(MousePosition))
            bHit = PlayerController->GetHitResultAtScreenPosition(MousePosition, TraceChannel, CollisionQueryParams, HitResult);
    return bHit;

The original function is in APlayerController.cpp:

bool APlayerController::GetHitResultUnderCursor(ECollisionChannel TraceChannel, bool bTraceComplex, FHitResult& HitResult)