Third Person Weapons - Correct Aiming

Hello,
I am trying to implement weapons in my test project.
This works fine, also for different weapons, but I got one problem: The Weapon is not exactly shooting to the crosshair.
The crosshair is a simple HUD Blueprint, which looks like this:

The weapon uses ray-casting. The weapon is added to the character mesh over a socket in the hand of the character mesh. The ray starts at the weapon location, but the forward vector of the camera is used as directional vector of the ray.
The blueprint looks like this:

http://fs2.directupload.net/images/150308/w9zhbdjq.png

And the outcoming result is this:

So the ray hits the wall at another point than the crosshair is indicating. And the difference is just too much to call it “accurate aiming”.

Has someone an idea what I am doing wrong, or how this should be done right?
Thanks in advance.

There isn’t anything wrong with how you’re currently handling this. The issue simply is that the ray you’re casting is intersecting with the wall before it can travel far enough to reach the crosshair. You can confirm this by firing your weapon in such a way that it doesn’t intersect with any geometry or meshes.

To fix this discrepancy you’d have to use an alternate crosshair setup that dynamically accounts for collisions like this.

You mean I should not place the crosshair in the middle of the screen?
But instead how? Moving according to the mouse?

I believe he is talking about “Convergence” As in, if you aimed at a target further away, then the 2 points would intersect in space at a certain distance. But beyond that distance they would cross paths with one another in the opposite direction then if the distance aimed at is too short. I think your placement of the gun in hand or the origin point of where the Laser sight is aimed needs to be adjusted. BTW I am not any kind of UE expert.

Actually, I just did some further testing on this and the trick is to to do two ray traces. Cast the first from the player camera perspective.

Then do a second trace using the impact point from the first as your end point and the weapon as your start.

Your exact setup will vary a bit, but the end result is right on the crosshair if you have it set at screen center.

1 Like

Thank you man.
That was exactly what I wanted to achieve.
Could you please write your comment as an answer so I can accept it?

Doesn’t have to. That’s what we’re for ;D

Can’t thank you enough…been trying the world to screen space route for the last couple of days…THANK YOU!!! like alot.

Thank you! Super helpful.

It might be easier to convert your Screen Space coords to World Space. This way you get the center of the screen as a starting point (Where your crosshair is) but convert it to world space so that it works in game.

Create a Blueprint Interface for your HUD called BPI_HUD with a Function called Get Screen Center then add an Output called ScreenCenterCoords and make it a Vector 2D. Then inside your HUD, add the interface and create a new variable called ScreenCenterCoordinates and make it a Vector 2D also. Then Set the ScreenCenterCoordinates variable after Event Receive Draw HUD, Split the Struct pin on the input pin and plug in the normal Size X and Y divided by 2 (to get the center of the screen).

Then double click on the GetScreenCenter under the Interfaces tab in your HUD and drag and drop the ScreenCenterCoordinates variable onto the input pin of the Return Node.

Finally inside your character create a function called ShootGun and inside this the first thing you want to do is get a reference to the player controller, then Get HUD then Get Screen Center from the HUD. Split the Struct Pin on the output and then from the player controller drag out and ConvertScreenLocationToWorldSpace and plug your X and Y from the GetScreenCenter node into the X and Y on the ConvertScreenLocationToWorldSpace.

This way you’re getting the screen center but converting it to WorldSpace which should solve your aiming issue.

Now you can use the WorldLocation as the starting point of your line trace and as normal, calculate the End point from the Camera’s World Rotation, Forward Vector, Multiplied by a Float for the distance of the trace and then add that to the World Location and plug it into the End of the line trace. Accurate aiming without the need for a double line trace.

Hi

I also have a similar problem with my line trace. I’m trying to achieve the same thing here but I made it a little different: I’m getting the impact point of the camera’s line trace and tell my muzzle to align its rotation to it. The result I get is the projectiles and the weapon’s line traces to go towards the camera instead of the impact point of my original line trace. I tried inverting the maths but it doesn’t seem to do the trick.

If anyone here has some experience with line traces and could explain to me what I did wrong it would be greatly appreciated. I would really like to learn from my mistake.

I think your issue may be caused by the fact your are using single ray tracing. It thus returns the first hit whether it is blocking or not. So because you are starting your trace from the camera, the camera is hit and this is your impact point. To get the impact point from the first wall in front of you for example, you should be using multi line tracing :

Multi line tracing will return you all of the hits up to the first blocking hit if there is one.
So you should check in the outHits array if the last hit is a blocking hit. If yes, this is your impact point, so you can continue with aligning the gun towards this impact point. If the last hit is non blocking, it means there is no blocking target in your raytracing range. So in this case you might want to pick the TraceEnd location as a fake “infinity” far impact point, which should look good if it is far enough from your pawn.