Problem Aiming Weapon To Crosshair / Center of Screen

I think I’m close but still have a problem. When I aim up 90 degrees then down to zero degrees all works fine like I want. Now when pointing the weapon down from zero degrees at around -57 degrees the weapon starts going back the wrong way.

1

I have this setup like pretty much everyone else, I use a line trace from the third person camera as the start then forward vector x 100000 for the end, then I use the FindLookAtRotation node using the weapon socket “Muzzle” location as the start location and the line trace hit location as the target.

I tried normalizing the vectors before feeding them into the FindLookAtRotation node and it almost worked, but around -88 degrees the weapon vibrated a lot.

I have been hung up on this problem for many months, I have googled so much my fingers are about to fall off lol. This should be something so simple but not many people giving their secrets out :slight_smile: . Also I do not want my projectile to just hit the center of the crosshair/screen the weapons muzzle must point to the center of the screen. I am just doing the pitch right now I will do the yaw after I figure this part out.

Thanks for reading.

I’m not sure if you’re using just the one line trace, but I found using two as a reliable approach to this scenario, but very similar to what you described.

First line trace is starting from the center of the view port forward to any specified distance and making a hit location variable.

The second starting at the weapon socket muzzle and ending at the hit location of the first line trace and ultimately where to pull off the information to apply damage, decals or particle effects. You can even add some space in there, so you don’t hit any objects between the camera and the back of the weapon.

As far as the rotation goes, I’m applying rotation in a different manner, but I would suspect that if the weapon is using the same line trace it’s mirroring the logic and it’s just offset to the side a bit and is slightly off.

I’m just using 1 line trace. Right now I’m rotating the weapon but in the end I will rotate the hand bone, this is just for testing until I get it to work right. How are you using both vectors to point the weapon at the center of the screen? Thanks

I’m more or less using the control rotation for the pawn and orienting the pawn in that direction, but the important thing for my situation was to get the correct direction from the muzzle from a 3rd person perspective. I’m sure you could easily get the rotation with this system and orient whatever in that direction.

I could never get a weapon(line trace) oriented from a different perspective perfectly accurate with a single line trace… Maybe there is a way, but I’ve never heard of it working correctly.

You can ignore the “Rifle” variable it is not hooked up… You might also notice the slight adjustment to the impact point as sometimes the line trace would fall slightly short of the collisions to get a hit. That adjustment was the cure to that.

This is what I have as a function. You have the freedom to use it anywhere, with different socket names, object references, and adjusting the range on the fly:

Could you do something like this? This is the blueprint of the little gun I mashed together:


I may be misunderstanding the problem you are having. This produces this though:

ezgif.com-gif-maker

Obviously this is from the first person perspective but it would work the same for a third person project just do the line trace with the third person camera.

From what you posted that’s how I want mine to work, I’m using the third person template and for some reason it’s close but a little off. I’m going to try it in the first person template and see if it works like yours. Thanks

No problem! Your blueprint nodes would work if you switched the way you calculate your direction to how I showed. The math is just:

directionVector = Normalize(hitLocation - startLocation)

Then just convert that vector into a rotator and set the actor rotation to that.

This is how close it is, maybe I’m to picky? lol Yours looks perfect though. I’m only using this aiming style for hip shooting so I might end up faking it and having the bullet spawn at the angle it needs to go like many others do, maybe no one will notice that little bit… at least the barrel is facing pretty close to where the bullet will be going. Let me know if you have any more ideas. Also why do you subtract the location from the actor on the hit result?

Thanks

Edit: It actually looks like the debug line trace from the camera is lagging behind a bit making it look off?

2

this might be what’s called “Gymbal lock”, in which case, i think you would have to use Quaternions instead of Rotators.

Quats are hard to figure out. Try converting everything to quats, then multiply the forward vector by your desired rotation. Just keep trying a bunch of combinations until you get it. Also, the order in which you multiply makes a difference.

Here’s are some nodes that could help you :

Unfortunately the lag you’re seeing is due to frame rate. Because I am doing the line trace in Tick, the rotation of the gun is entirely frame dependent.
Here’s what it looks like in 60 fps:
ezgif.com-gif-maker (1)

And in 120 fps:

ezgif.com-gif-maker (2)

I think a way around this would be to create an action mapping and a look input action, then call the line trace and update rotation from there. This way the call would be asynchronous (I think input actions are frame independent). If they aren’t asynchronous then I’m not sure how to combat this.

As for why I subtract the hit location from the actor location, that is to get a vector from the actor location to the hit location. If you consider the actor only on the x-axis at location (5, 0, 0), and the hit is at (8, 0, 0), the vector from the actor to the hit is (8, 0, 0) - (5, 0, 0) = (3, 0, 0). To get the direction vector normalize this which would = (1, 0, 0).

You can remember getting a vector from somewhere to somewhere like writing a letter. When you write a card or letter its to ____, from ____. Same thing for vectors, you do: (to hit - from actor) = (hit - actor).

I advise you to look up how to find the vector between two points for more information. Here’s one stack answer explaining it:
https://math.stackexchange.com/questions/1658533/find-direction-of-the-vector

This is a thanks to everyone who helped me on this, I think it’s good enough where I’m happy with the results. Aejjee you saved the day! I got it to work using half your code and half of what I was using with the FindLookAtRotation node and FInterp To nodes. This function gets fired in the characters anim graph off the Event Blueprint Update Animation node

Result
4

1 Like

That looks good, you may find more optimizations in the future too. I’m glad I could help and good luck with your project!

1 Like