Third person shooter line trace

how can i center the line trace from the muzzle of the weapon to the center of the reticles(Screen)?

Basically you need to calculate the rotation required for the shot to spawn from muzzle and intersect the cameras center screen. So line trace by channel (visibility) from camera out to 10,000 cm or so (10m). If the trace hits something take that “impact point” and find the look at rotation from muzzle to that point. Otherwise use the trace end point for the find look at rotation. Make transform … pass the muzzle location to “location” and the find look at rotation as the “rotation”.

My project has 3 cams. FPP, TPP and ADS. “Projectile camera” is the camera being used at time of shot.

Hi! Can you tell more - what actually do you want to achieve? If you just want to get the world location of the center point of screen, then it is not too difficult:

const FVector2D screenPos = ...;
FVector worldPos;
FVector worldDir;

if (auto controller = UGameplayStatics::GetPlayerController(world, 0)) {
	if (UGameplayStatics::DeprojectScreenToWorld(controller, screenPos, worldPos, worldDir)) {
		// DO YOUR STUFF
	}
}

I realize it’s been a while, but how would this work? If you’re performing the trace from the camera, then you’re not actually tracing from the muzzle, you’re tracing from the camera. So you’re not tracing from the muzzle, you’re just getting the look at rotation from the impact point to the muzzle. What I don’t understand is what is the purpose of doing this?

I would have simply answered that you don’t need to line trace from the muzzle on the gun. Line traces happen from the player’s camera out into the world. Then you would spawn your projectile from the muzzle location to the impact point.

Clipping!
The shot, either Projectile or Trace, should always start at the muzzle. If my muzzle is blocked then the shot should hit at that location.

Imagine standing behind a stack of boxes. 3rd person camera can see over the boxes. You don’t want the shot to hit the target. You want it to hit the boxes at muzzle height.

The trace gives us a rotation to hit the center screen.

Are you adding 0 to the pitch in the “zeroing pitch” node? What is the reason for that?

It’s a simple approach to toying with near and far zeroing.

You mean you would not leave it at zero but add different values depending on the zeroing range?

Correct. Adding Z rotation will add more arch for a further “Far Zero”. This will also add a near zero.

1 Like