how to spawn projectile from weapon using sockets

hello all. i have two weapon. i want to spawn projectile from my weapons. first i want to add socket.i will use ray to crosshair. and my bullets will go to that direction. thank your for helps

Hello.

I did not understand what you’re trying to do ? I don’t get your drawing, can you detail a bit more how you want to calculate the direction ?

If your crosshair is centered on the screen, line trace from camera and use the hit location (or Trace End if it misses) and find lookatrotation for spawning projectile class.

For the socket location there, I used a mesh (called WeaponM in teh bp) attached to char’s hand which has its own muzzle socket. IF you char mesh has the guns baked in, add the socket to the characters skeletal mesh and get socket location from that.

1 Like

No need to do a trace for shooting a projectile from a muzzle location toward center screen.

Variables and MATH.


In the projectile class create a vector variable ProjectileSpeed set it to Instance Editable and Exposed on spawn.

In the construction script take PMC and Set Velocity


Pass Aim Velocity to the spawn actor of class node for your projectile.

1 Like

That works well when the camera is mostly horizontal, but when using camera forward * some arbitrary float, weird things can happen when aiming high or low. Like if aiming at the top edge of a wall, the crosshairs could be on wall while cam forward*2000+loc could be over and behind the wall in Ohio somewhere. Linetrace from camera pretty much assures you’r hitting what’s in the center of the screen. And as an added bonus, one could use the hit actor result to fire off interfaces functions or something on target to warn them if need be. EG, with a slow projectile, or shooting at Neo from the Matrix one might want AI to consider trying to dodge.

1 Like

I use it all the time. It’s 100% deadeye to whatever I’m aiming at. Works 100% with Aim Offset as well.

1 Like

I don’t have a separate weapon. The weapon is attached to the skeleton. So how can I get the socket’s location? I added sockets to the ends of both weapons using the “add socket” command. I want to use these locations in my blueprint. thank you for helps i ll use that math later

whats wrong here. maybe my socket location? i need to shoot from 2 weapon btw

If I remember correctly here, the easiest way to do this is to make your projectile bp, add a projectile movement component (turn gravity off it you don’t want it to drop).

And then place your socket on the weapon. When you go to fire spawn actor at location, choose the socket, and plug in your rotation and then it should be right.

Use get socket location and pass that vector as the spawn location.

2 Likes

Dunno why the projectiles are doing that without seeing the whole function. Socket looks fine tho, To fire with both guns, you could do somethign like this on the input action event that calls the shooty function:

In the shoot start function,I added a name input, and when spawning projectiles, I get the socket name local var from the input to feed the get socket location command:

Doing it like this would alternate fire between the two guns.. Could be a headache to track ammo, but, yea.

1 Like

thank youu it works. now i need to rotate my weapons towards crosshair bro

thank u sir

i have new question. when i aim up a bit more bullet going wrong.