Adding weapon to character hand

I want to add a gun to my character’s right hand, however, I don’t see that gun anywhere near the character. I tried adding the gun group as a child to the hand, and then setting the parent of the gun to that hand. It didn’t work. How do you add a weapon to my character’s hand? If it helps, my character is a mixamo character.

fbxLoader.load("soldier.fbx", function (fbx) {
  fbx.scale.setScalar(0.01);
  let playerBox = new THREE.Box3().setFromObject(fbx);
  scene.add(fbx);
  const playerSize = new THREE.Vector3();
  playerBox.getSize(playerSize);
  const playerGeometry = new THREE.BoxGeometry(playerSize.x, playerSize.y, playerSize.z);
  const playerMesh = new THREE.Mesh(playerGeometry);

  fbxLoader.load("pistol.fbx", async function (fbxPistol) {
    fbxPistol.scale.setScalar(0.01);
    const rightHand = fbx.getObjectByName("mixamorigRightHand");
    rightHand.children.push(fbxPistol); 
    fbxPistol.parent = rightHand;
    console.log(fbx);
  }
}

When you add a 3D model as a child to a bone, all transformations of the bone are also applied to the model. So you might have to update the model’s scaling, position and/or rotation so it appears with the correct transformation.

BTW: Don’t do this:

There is a dedicated method for this use case: rightHand.add( fbxPistol );

1 Like

I tried your solution, and then attempted moving the model. After playing around with some code to get fbxPistol in a better position using fbxPosition.position=… I realised this doesn’t seem to change the position at all. Any ideas how I can apply transformations to my model that actually affect it?

I was trying to load a sword in right hand but I don’t see my sword after following the implementation. Please see my code below. Could you shed some lights on this please?

            fbxloader.load('models/fbx/guard/vanguard_t_choonyung.fbx', (fbx) => {
                actions = {};
                fbx.scale.set(0.01, 0.01, 0.01)
                fbx.position.set(0, 0, 0)
                guardMixer = new THREE.AnimationMixer(fbx)
                actions['default'] = guardMixer.clipAction(
                    fbx.animations[0]
                )

                fbx.traverse(function (object) {
                    if (object.isMesh) object.castShadow = true;
                });

                model = fbx;
                model.rotation.y = 3;

                skeleton = new THREE.SkeletonHelper(model);
                skeleton.visible = false;
                scene.add(skeleton);

                dirLight.target = model;
                fbxloader.load('models/fbx/guard/animations/Standing Idle.fbx', (fbx) => {
                    actions['idle'] = guardMixer.clipAction(
                        fbx.animations[0]
                    )
                    currentAction = 'idle';
                    activatedActions.push(currentAction);
                    actions['idle'].play();
                    fbxloader.load('models/fbx/guard/animations/Great Sword Walk.fbx', (fbx) => {
                        actions['walk'] = guardMixer.clipAction(
                            fbx.animations[0]
                        )
                        scene.add(model);
                        guardReady = true;
                    })
                })

                fbxloader.load("assets/weapons/fbx/sword/Scottish_Sword.FBX", function (fbxSword) {
                    scene.add(fbxSword);
                    fbxSword.scale.setScalar(0.015);
                    const rightHand = fbx.getObjectByName("mixamorigRightHand");
                    rightHand.add( fbxSword );
                })
            })