im having some trouble figuring out how to get my projectile that spawns from my weapon barrel to fly towards the crosshair and not exactly where the animations like idle and sway influence the guns direction. any suggestions?
APawn::GetPawnViewLocation() and APawn::GetPawnViewRotation() is what you should use.
By default, they will give you the location and rotation of the centre of the pawn’s eyes (I believe). You can override this behaviour if your crosshairs work differently.
You can then get the forward vector by using the GetForwardVector node on PawnViewRotation and multiplying by whatever.
Direction = PawnViewLocation + Forward.
For more accurate results, you can pass that information to a LineTrace.
Trace from camera out a few meters. Using the hit result (return, Impact point, trace end) do a select to determine which to use. Get weapon muzzle and Find look at Rotation from it and the return from the select. Make transform (loc:muzzle location, Rot: Find look at rot).
This will work regardless the camera position… FPP, TPP, ADS etc
Just to note this is a simple “Client side” only approach. If you’re building for multiplayer there are a few changes needed to make this work with the server. More so to have the servers trace match up with the clients.
Here’s the MP version w/added projectile velocity option. e.g. if your projectiles use “velocity” instead of “Initial Speed”.