Download

how to attach gun to character without arms / mesh?

Hello everyone!

I am trying to make my first game ever!
I made my character in c++ but it currently doesn’t have any mesh and if it would get a mesh it would be a simple capsule.

What i want to make is a gun that is attached to my body but it floats in the air (like the picture below).


How can this be done so that it floats in the air and even when i crouch the gun goes down together with the camera?

Thanks in advance!

Look into sockets. You would create a socket on the actual mesh where you would want your weapon. Then with the attachtocomponent function, there is a optional field where you can tell it to attach to the socket FName if it exists.

The other option is to attach it to the camera if its a first person shooter.

You should add a scene component called Gun Position Component and attach the gun to this component as a child. Or else you can also try adding a Socket to the player skeleton and attach the mesh to the socket

Thanks!
It is indeed a first person shooter, so I will have a go with attaching it to the camera!
But for the sockets, Can I add it to a socket even though i don’t have a mesh for my character?
Or a mesh that would be a capsule or a ball where you don’t have arms or hands to attach it to?

I don’t believe you HAVE to use sockets, but you can. Sockets are generally a visual thing you apply to a mesh. I believe you can spawn sockets in without a mesh but I havent tried it.
If you do a attachtocomponent or attachtoactor, it will keep whatever xyz coords you spawned it in. so if you spawn at 0,0,0 then do an attach, well that is now attached dead center to your other pawn. if you attach 50,0,0, well now its always gonna be x+50 compared to your pawn/actor.

if you dont know what xyz to use, you can use the unreal engine editor. Drag your item in the editor and “attach it” to your camera (if you have on already spawned in the word/editor)
then move it where it should be (if you have your pawn/actor selected and it has a camera, it should create a first person preview though the camera) move the weapon where it needs to be so the preview window looks good to you. then notice the x,y,z numbers. Use those numbers for you code when you spawn it in, then do the attach to actor/component, whatever you ended up attaching to in the unreal editor. hope that makes sense.