What should I pay attention to when using attachToBone?

I used attactToBone to bind a box to the dude in the demo. I moved the dude but the box didn’t follow the movement when the bone didn’t have animations.

BABYLON.SceneLoader.ImportMesh(“him”, “/scenes/Dude/”, “Dude.babylon”, scene, function (newMeshes2, particleSystems2, skeletons2) {
var dude = newMeshes2[0];

    var box1 = BABYLON.Mesh.CreateBox("Box1", 10.0, scene);

    var materialBox = new BABYLON.StandardMaterial("texture1", scene);
    materialBox.diffuseColor = new BABYLON.Color3(0, 1, 0);//Green
    varterialBox2 = new BABYLON.StandardMaterial("texture2", scene);
    box1.material = materialBox;
    box1.attachToBone(skeletons2[0].bones[1]);

    dude.rotation.y = Math.PI;
    dude.position = new BABYLON.Vector3(0, 0, -80);
        
});
1 Like

Hey @ergou_li ,

I had the same problem some while ago. Here’s the thread:

Here’s the solution were the Pros came up with:

I hope that helps :slight_smile:

From my two cents. I think in your code the box1.attachToBone is missing the second parameter. A reference to a mesh. Here’s a link to the documentation

3 Likes

Thank you very much for your reply. I made the following changes in your example like this, the animation stopped and the sphere did not follow the human movement. The person removed the sphere in place, but the position of changing the sphere will follow the person.

   sphere.attachToBone(skeleton.bones[34], dude);
	sphere.scaling = new BABYLON.Vector3(5, 5, 5);
	
    scene.beginAnimation(skeletons[0], 0, 100, true, 1.0);

    setTimeout(()=>{scene.stopAllAnimations()},5000);

    setTimeout(()=>{sphere.position.x = 0.001},10000);

	scene.registerBeforeRender(function () {
		dude.position.z+= 0.005;
	});

hey @ergou_li ,
not sure if I can help you with following problem, since I’m also fairly new.

But if you create a simple playground, it would be much easier to understand and help you with your problem.

You can create a playround over here: https://playground.babylonjs.com/
Hit ‘save’ and share the link in this post :slight_smile:

1 Like

Thank you for your help, I will create an example

https://playground.babylonjs.com/#N4T4AJ

I want to kown why the sphere didn’t follow when the dude stop animation.

Because the bones are not moving. You have to move the root bone and not the mesh if you want to achieve your goal:
https://playground.babylonjs.com/#N4T4AJ#1

But wait a bit as there was a bug that I’ll fix for next commit (in a hour or so :))