Line Trace doesn't hit everytime

Hello, I’m trying to create a top down shooter, and for that, i want to shoot from the muzzle to the position in world where the mouse has clicked.

I tried a lot lot connections, but now i’m asking for help : my issue is the end of the line trace sometimes hit, and sometimes not. Even on the same surface, without moving etc (but when i click few times on the same pixel : somewhere it works, it always works, and somewhere it doesn’t work, it will never works).

If you need more info, don’t hesitate :slight_smile:


Hey @Kiame!

One thing you can do to debug this is break the out hit struct pin!

Disconnect the out hit line, and right click the pin to break it. Then you can get a ton of information! You will likely need the location of the hit, though you can also get the hit actor and check if it’s valid then do stuff if it is (like if you hit an enemy actor) or use the location to spawn bullet holes etc.

Not sure if it can help you but keep in mind that if you have a HUD, 2D sprites may block the trace and return a hit. You may have to avoid it by telling your UI elements not to block traces :wink:

You are doing two separate traces probably leading to strange results.
If you’d get the cursor position from the playercontroller, then use the following node " Deproject Screen to World":

You get the position of the cursor in 3D world space, and a direction it points in (forward from view).

To trace from the cursor, feed the following data from that node into the Start and End pins of the LineTraceByChannel:

Start: Position
End: (Position + (Direction * Trace length))

If you wish to trace from sphere collision position to cursor position just use both positions as start and end point without further additions.

Like that ? Because if yes, it gives me that result (i made 6 mouse click for the example)


I also move it from my function in my BP weapon to the BP of my character (because the function sent always no hit (XYZ coordinates at 0) so i have another issue that not concern this topic haha :smiling_face_with_tear:

1 Like

Hello ! Yes that’s what i do right after my function :slight_smile: I add a picture of what i get to be more precise with my issue

Looks like some of the traces end just before hitting the wall. I assume this is caused by “Get Hit Result Under Cursor by Channel” returning a location that’s ever so slightly offset from the wall.

You can confirm if this is the case by either un-possessing the character in editor image or enabling debug camera and looking inside the wall to see if the traces that didn’t hit are there or not. If all the Traces inside the wall are green, you can confirm this is the issue.

If that is indeed the issue, a possible fix is to extend the End input a bit into the wall. You can do this by getting the direction between the Start and End, then multiplying it by 10.0 [EDIT] then adding it back to the mouse hit location and using it as the new End input.

1 Like

Seems to be that, but noob question : how to do that ?

What i found on Reddit and UE forum leads me to do the following but it’s wrong



you have to add the extension (unit direction x 10) back to the end result (mouse hit location) and plug that into end

2 Likes

Like in my last screenshot ?

Thanks @Auran131, I forgot about the add.

Yeah

1 Like

Thanks you all for the help !

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.