[SOLVED] Attaching gun to hand bone

Hello

I have been playing around with creating first person hands by downloading some free models. What I want to achieve (or rather understand) is attaching a gun to the hands so that it they become together. I have searched all the discussions in this forum and watched some unity guides and managed to get it working but I am still getting problems with the rotation of the gun not being done properly.

I am doing something wrong but I can’t figure out what.

Here is link to project: https://playcanvas.com/editor/scene/1295567

Thanks :smiley:

Hi @nasjarta! An easy solution is to attach the gun in the editor and keep it disabled. If the gun is picked up then you can enable it.

1 Like

There seems to be a issue that the rotation of the gun is never the same in the world. For example I rotate gun in editor but the rotation is completely different in the actual scene on launch. The gun is connected but the rotation is keep on changing.

Do you mean by attaching the gun to hand manually in a model editor then creating multiple hands for multiple guns and enabling/disabling them for whichever one is selected?

Also here’s the logic I found on the forum and tried to implement it

ArmsScript.prototype.initialize = function() {
    this.arms = this.app.root.findByName('arms');
    this.gun = this.app.root.findByName("gun");

    this.handBone = this.arms.model.model.graph.findByName("ik_hand_gun");
    this.gun.reparent(this.handBone);
    
    this.scale = this.entity.getWorldTransform().getScale();
    this.gun.setLocalPosition(pc.Vec3.ZERO);
    this.gun.setLocalScale(1/this.scale.x, 1/this.scale.y, 1/this.scale.z);

};

No, not multiple hands, only multiple guns. :upside_down_face:

Be sure you don’t use any code that rotates the gun.

Yes, that’s an alternative solution.

1 Like

Solved it with :smiley:

this.gun.rotateLocal(90, 0, 0);

When rotating it manually in the editor I could never get it to the correct position, was very weird but 1 line of code fixed it.

Just 2 noob questions I have @Albertos when I put models inside the editor sometimes they come out too big or too small. Is it possible to control how they auto-size when you drag them into the scene? or do you just need to adjust scale manually via editor/code to your liking?

and also from what I understand the best way to achieve fps hands would be to have some hand animations like 1 for assault rifles, 1 for pistols, 1 for knife etc and then attach weapons to them accordingly?

That sounds illogical to me.

As far as I know, no.

Yes.

That could be a solution if you don’t have animations that control the hands.

1 Like

1

Was happening because I was attaching the weapon to the ik_hand_l and ik_hand_r nodes, the rotation is very bugged when attached directly to one hand. When I attached it to the parent ik_hand_gun it worked.

So it works with 1 model I have perfectly, I tested another model but then the same rotation issue happened again.

I think the problem is because the model that works is like this

2

The model that doesn’t work looks like this

3

6

As you can see on the AK the weapon is way off from the original source there is a big gap. When I do the same thing with this model the gap is visible even though the position is correct. Is this a problem with the model itself then? Or is there a way to fix this? I have the same problem with the scope that I want to attach to the first model, but theres a gap like this.

4

5

Thanks

Yes, you can solve this by adding the model entity as a child of an empty parent entity. Position and rotate the model entity so that it is centered of the parent entity.

1 Like

That’s a nice trick I didn’t even know this. Thanks!

I managed to get it to work, playing around with it’s position and rotation for some time.

1

The unfortunate thing is now the other model is rotated completely wrong, so does this mean I have to make different rotations for all different weapons then? so that all are aligned correctly with the hands?

Also with this model the hand clips through the magazine. So is the best way to overcome this to either position/size it as best I can or to just find another arm, animation or gun to achieve the best possible look?

Other models that works correctly before should not be effected by this solution, otherwise you do something wrong.

Yes, you have to set the right position and rotation for every weapon.

Yes, that’s correct. Maybe you can also solve this with using layers, but I have not enough expierience with this.

1 Like

The model is positioned based on its mesh origin. That gap you see is caused by the fact, that the mesh origin was offset from the model, when it was exported from 3D modelling application (Blender?). What you can do is to open Blender and shift the origin of the model to the location, where the gun is supposed to be attached to the bone. This way, when you add the model to the arm bone, it will correctly position itself, and you don’t need to do parent/child adjustments. Same thing for the scope and other attachements.

1 Like

Thanks for the answers and tips guys. Was really frustrated yesterday but today I learned a lot :smiley:

1 Like