# How to determine a cube's: right,left,etc through line trace?

So heres the thing, I have an actor(it has a simplified box collision) with 6 sockets. 1 for each side: up,down,left,right,etc. I want to be able to ray cast and when it hits the actor I want it to check which side it hit and if it hits the right side of the cube I can get the right socket’s transform and spawn another actor.

In short, I just want know which side the ray cast hit in a cube.

I’ve tried many things and it has work but it drained performance and so i want to know if there is a way more efficent way?

Sorry for the bad english, I hope you’ll help me!

Hmm, I’m not an expert on that level but maybe you could create 6 box collisions, one for each side, and set them to block linetraces in their collision settings. Then in the line/ray trace, you check which component of those 6 box collisions you hit, and if you hit the box collision which is named “Left” for example, you know you hit the left side…

Take the dot product of the axes of the cube and the hit normal.

The hit normal is a vector in the direction of the surface that was hit.
The dot product of 2 vectors gives you the cosine value of the angle between the two vectors.

Eg. if you have 2 perpendicular vectors, the dot product will return 0. (Acos(0) is 90°)

So, to check if your trace hit the front or rear of the cube, take the X (forward) axis (a vector pointing in the direction of the cube’s front) of the cube, and dot it with the hit normal.

If it’s 1, the difference between the hit normal, and the forward vector is 0°, which tells you that it was hit on the front.
You may want to give some leeway, instead of relying on the hit normal being absolutely perfect (and thus hitting 0.0000°).

Below is a simple macro that uses a high value of 45°.

Let’s call it GetHitSideSingleAxis.

The function takes the dot product of the passed in vector (Eg. X (forward)) and the hit normal. If the hit normal is pointing in the same direction as the forward vector of the cube, the front was hit. If the hit normal is pointing in the opposite direction of the forward vector, it was hit in the rear.
0.7071 is approximately 45°
In case of X (forward), the macro will execute either Positive (forward), Negative (backwards) or neither, if the hitnormal is pointing neither forward nor rear.

Making a simple enum with all the sides (None,Front,Rear,Right,Left,Top,Bottom) we can make a simple function that returns which side was hit, given the axes of the cube, and the hit normal from the trace.

Let’s call it GetHitSide

The axes can be retrieved by getting the rotation of the cube, and using the GetAxes node.

In case you’re having trouble understanding the logic behind this:

1 Like

That was the method I was using before. The thing is the next actor I spawn I also want to check the six sides , and as soon as I I get to 40 of the same actor frames start to drop, so yeah I don’t want to use that.

Sounds legit , thanks for the answer , I’ll work on adding it soon and then reply how it went.

Ok so the good news i can finally clasify the side’s but the bad news is this:

Error Blueprint Runtime Error: Accessed None trying to read property CallFunc_BreakHitResult_HitActor from function: ‘ExecuteUbergraph_Character’ from node: Get Hit Side in graph: EventGraph in object: Character

But i fixed by just casting to the cube i want to hit. I’m really grateful for your help , ive been trying to do this for 2 weeks. Thanks!

No problem.

The error is probably because HitActor is None. Instead of casting it, use the IsValid node.