Third Person Shooter - Aiming at Crosshair

A bit late, but I was thinking of this and found the answer right after stumbling here.
Sorry for necro, I hope this may help any poor soul in the future.

The problems are:

  1. Tracing from the gun’s location to the camera’s forward doesnt work because sometimes it doesnt hit where it should (some weird offset). This becomes more apparent the further away the camera is from the gun on the X axis.

  2. Tracing from the camera location to the camera’s forward also doesnt work because the camera may be too far ‘behind’ the player and may be intercepted by something in between.

The solution:
What we need to do is “project our camera’s position onto our gun”. Like merging both positions.
Our trace ends up looking like this:

include “Kismet/KismetMathLibrary.h”

traceStartLocation = UKismetMathLibrary::ProjectPointOnToPlane(camera->GetCameraLocation(), GunLocation, camera->GetActorForwardVector());
traceEndLocation = traceStartLocation + (camera->GetActorForwardVector() * Range);
do the trace.
If it something, to be really sure nothing else is in the way; then trace from the GunLocation to the TraceHitLocation.

Precisions:
My aiming crossair is at the middle of the screen.
My Character, gun and Camera are set as in the image.

3 Likes