How can I detect which face of a cube was hit by line trace?

Ok, I know I’ve had lots of questions here lately but I don’t know where else to go. Iv’e done ALOT of research last couple days and it seems I cant do what I want.

I have a LinetraceSingle() returning a HitResult and I can determine which actor was hit no problem.
I have a Cube blueprint and In that blueprint I want it to recognize it was hit by the trace, and which face was hit.

The line trace comes from a C++ actor component attached to the player.

With a FVector_NetQuantizeNormal, I can store a HitResult.Normal, but only in world space. So if the cube is rotated I get different results.

Another problem with this is all of that is in my linetrace function. I’m having a hard time figuring out how to simply tell the hit actor it was hit, then preform whatever action
its supposed to do from within its own class (or blueprint).

I’m far from a “good” programmer so I apologize if what I’m asking makes little sense. Unreal C++ tutorials are pretty limited.

Thank you.

PS: If anyone out there wants to look at my code and help me fix fundamental problems with it, I’d be more than happy to compensate you with whatever seems fair :slight_smile:

You could just get the rotation of the cube, then unrotate the normal using that. You’ll end up with a normal relative to the cube, which seems to be what you want

You think that may be the easiest way? just seems like alot of hoops for what its doing

You could make each side of the cube a different object, but I’d imagine that this would complicate things in other areas.

Another possible solution would be to get the UV coordinates of a HitResult (never used it but heard it was added in some UE version).
From UV coordinates you’ll be able to figure out which side the line trace hit.
Not sure if that is computationally faster then unrotating normals, but can give you more interesting options.

You can easily do this by converting the hit location (which is in world space) to local space (relative location of the cube). Here is an example I made for you.

In your cube blueprint you need to create this:

Then in your character (or wherever you want) you can trace like this to fetch the face ID.

You can download the project here (4.17):…U52eFlaRDBYcjg

For this you should consider using Interfaces.

Hope this helps. :slight_smile:

Wasnt able to find anything is FHitResult involving UVCorrdinates. ryanjon2040 Your answer is exactly what I need and thank you for the project :):slight_smile:

you could also use the dot product of the hit normal so you don’t have to involve the cube size.

Yea, I keep coming back to simply doing a dot product on each axis, the lowest absolute value one would be the direction then just map that to the appropriate face. Haven’t sat down and worked out the actual math, but I think that would work.

EDIT: You’d also need the raw dot product value to determine which face along the axis was hit, but you would have that already while doing your compares.

Also think about getting the faceIndex (triangle) and the primitive mesh component. If you construct your own mesh or know how the mesh (always) looks like, this will allow you to understand which face exactly got hit. If I create a mesh, I usually use a special array stating additional information about the triangle. This way I can easily look up what exactly was hit. Once you know the triangle you can also determine where on this particular triangle the hit occured. This is one way for example you can calculate a decal.