Line Trace From Camera Inaccurate

I’m working on a project I created using the first person template in 4.7 and following along with this tutorial: https://docs.unrealengine.com/latest/INT/Gameplay/HowTo/UseRaycasts/Blueprints/index.html

I am doing a single line trace from the first person camera exactly as shown, however it seems to be consistently inaccurate. I can tell from the HUD blueprint that the crosshairs are being drawn directly in the center of the screen, however, the line drawn from my trace always appears somewhere outside the crosshairs even when an object is directly in front of the character. Any ideas what is going on here?

Could you show us how your BP looks like? It’s rather hard to blindly guess :wink:

I set the float I am multiplying the forward vector with to 1000000 and it seems to be a lot more accurate, but it’s still not perfect. I don’t really understand why that would be though. Here’s a video showing the result, you can see the hit location is still just slightly off, with a smaller value it is much less accurate:

Looking at the blueprint the End Location is starting at World (0,0,0) + CameraRotation x Distance. For the End Location try doing GetCameraLocation + CameraRotation x Distance.

Is this what you meant? I can’t tell what’s going on, but the trace is happening behind the character instead from strange angles as a result.

No, but close. Multiply the distance by the camera rotation forward vector then add that to the Camera Location.

Doesn’t seem to make a difference, it appears equally as accurate as I had it previously. It seems to work well enough for what I need it to do, maybe this is just as good as it gets.

Here is a working example I tested. If your setup is the same and still not working correctly then your camera location or rotation might be returning incorrectly and you will need to debug it. You should be able to get a trace directly down the Crosshairs / Center of Camera with the example provided.

To debug if needed right click in the blueprint and do a search for drawdebugsphere. Create multiple drawdebugsphere nodes and plug in the vector values you have set for the start and end traces. Execute it when the trace happens and see where the start and end locations show up in game.

1 Like

Confirmed that Skylar’s advice works… though the blueprint provided I couldn’t replicate exactly.

See mine attached.
Epic should update the document to add the addition portion.

Have you checked if the center of your crosshair image is exactly in the center of the screen?

If you are starting from the default First Person Shooter template, the HUD blueprint draws the crosshair at half the width and height of the screen. But, this is referring to the top left corner of the crosshair image. If you want the center of the crosshair to be in the center of the screen, you need to offset it by half the width and height of the crosshair image.

In the case of the Default First Person Shooter template, since the crosshair image is 16x16, you need to offset it up and left by 8 pixels.

1 Like

Ja, Ziss ist zee korrekt answer

This is the correct answer. I had the same problem and now it’s fixed, the line trace hits exactly in the center of the crosshair.

EDIT: In fact all the correct answer is a combination of all the answers in this thread. To get the correct behavior: The end vector for the trace should be CameraWorldLocation + ForwardVectorXLength, and you should also adjust the HUD blueprint to draw the crossahir exactly in the middle of the screen by substracting half width and half height.