Sure thing, I can send you the project. But one last thing, could you double check the material slot name and material name in your mesh - they need to be the same for this to work properly:
Sent you the project. Good luck!
Thank you very much. 么么哒!~
This was never officially solved and I am experiencing the same issue in UE 5.3. The hit item is -1, element index is 0, and face index is -1. But it’s the correct mesh as when I print the object name it’s the right one. And I have the static mesh as the root of the blueprint, not that it should really matter.
Edit: I’ve figured it out. Well, kind of. It’s a limitation of using blueprints for raycasting. None of them allow the bReturnFaceIndex
variable to be set to ‘true’ from the default/initial ‘false’ value, which in turn causes it to always be -1. If you walk through with a debugger, you can see the internal face index is correctly changing and the object you’re hitting has the proper material slots.
I guess the only solution for now is to write your own function via C++. Would be nice to have a parameter in a future official build though.
Here is a complete C++ solution.
I basically modified the original function so that the caller has a control of setting the related params:
bool AMyGameGameMode::GetHitResultUnderCursorForObjects(APlayerController* controller,
const TArray<TEnumAsByte<EObjectTypeQuery> >& ObjectTypes, bool traceComplex, bool returnFaceIndex, FHitResult& HitResult)const
{
ULocalPlayer* LocalPlayer = Cast<ULocalPlayer>(controller->Player);
bool bHit = false;
if (LocalPlayer && LocalPlayer->ViewportClient)
{
FVector2D MousePosition;
if (LocalPlayer->ViewportClient->GetMousePosition(MousePosition))
{
// Early out if we clicked on a HUD hitbox
if (controller->GetHUD() != NULL && controller->GetHUD()->GetHitBoxAtCoordinates(MousePosition, true))
{
return false;
}
FVector WorldOrigin;
FVector WorldDirection;
if (UGameplayStatics::DeprojectScreenToWorld(controller, MousePosition, WorldOrigin, WorldDirection) == true)
{
FCollisionQueryParams collisionParams;
collisionParams.TraceTag = SCENE_QUERY_STAT(ClickableTrace);
collisionParams.bTraceComplex = traceComplex;
collisionParams.bReturnFaceIndex = returnFaceIndex;
FCollisionObjectQueryParams const ObjParam(ObjectTypes);
return controller->GetWorld()->LineTraceSingleByObjectType(HitResult,
WorldOrigin, WorldOrigin + WorldDirection * controller->HitResultTraceDistance, ObjParam, collisionParams);
}
return false;
}
}
if (!bHit) //If there was no hit we reset the results. This is redundant but helps Blueprint users
{
HitResult = FHitResult();
}
return bHit;
}