Line trace: how to get hit material?

I would like to get which element index (or, which material) was hit on a line trace test. From what I’ve seen this is currently not supported by the engine.

I’m currently digging into engine source to see what I could change to support this, but apparently only the physical material is saved in physx (which is what does the traces).

Any help on retrieving this information (hit material or element index)?

Epic must have a way to do this, because think about it

if you have a skeletal mesh with multiple material indicies, and you drag a material directly onto a certain part of the skeletal mesh, the material is applied to just that index!

Maybe look at where that is happening to get more info?

Let me know!

Yeah, drag and dropping from the content browser onto a static mesh works too. Any idea where that could be implemented?

Apparently it starts in LevelEditorViewport.cpp FLevelEditorViewportClient::DropObjectsAtCoordinates()…

Take a look at FCollisionQueryParams. There is a flag called bReturnPhysicalMaterial which defaults to false. In some places in code we set this to true, and you should be able to then get it directly from the FHitResult.

Hope that helps

Hi Ori,

Thanks for the answer, but that’s for getting the physical material. I want to get the (rendering) material, or the mesh’s element index.

i’m looking for the same thing…

As Rama & Ixiguis hinted, take a look at the “HActor” hit proxy code - that has a MaterialIndex which gets set e.g. in FSkeletalMeshSceneProxy::CreateHitProxies.

However a lot of that code seems to be editor-only, bear that in mind depending on what you’re trying to do. I think maybe HitProxies might not be fast enough to use in-game, because they involve re-rendering the scene to generate the hit proxy map (see FViewport::GetRawHitProxyData()).

why not just break the “hit result” of the line check, get the “hit actor” or component maybe and “Cast to StaticMesh Component” then you can get material on that. Or you can get number of materials and do a loop to get each material.

This does not seem to work when a mesh is composed of only one component, but has multiple rendering materials—at least for grabbing the one specific hit material.

first of all Break Hit Result from LineTrace and get FaceIndex , Hit Component.
then you need function Get Material from Collision Face Index

you can get from it value (and name) of hit material, then just check all materials with the same name.
In the end you get material index of this static mesh, that you can change in the future.

5 Likes

Hi, Can You please Share the whole blueprint. Actually I am new to Unreal Engine 5.3. And I have to do this Task.