How to get the projectile to fly to the crosshair?

hello all

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?

please note im using a template called fpsm the majority of the blue print code is the creators

Check this thread, maybe it will help.

I’m not sure if that works as I haven’t it tried myself, but you coul try it. It’s simple math you can easily make a test.

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

4 Likes

Thank you so much for this. I struggled for days, and had no luck with any other video or forum explanation/tutorial.

Thanks :sweat_smile:

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”.

Returns:
Transform (Muzzle Location, Look At Rotation)
Normalized Vector (projectile trajectory vector w/out muzzle velocity)
Velocity Vector ( Normalized Vector * Muzzle Velocity)