I’m sure I’m not the first person who asks about this since this is a rather common operation (I think), however, neither Dr. Google nor the Unity Scripting API search produced any results.
What I would like to do is attaching a mesh to another - using Javascript. For example, I have an (animated) mesh of a knight and I want to be able to exchange his weapons without having to substitute the entire model. So, I would have to attach the weapon object to the knight object - to its right hand, to be precise.
Every engine I have been working with so far had its very own approach towards this topic (e.g. using vertex identifier numbers from the right hand of the model to calculate its world space coordinates and put the weapon in that place).
I’m sure there must be something for that purpose in Unity, its just difficult to search for it when you don’t know what it is called ^_^’
Typically you’d use two models, one for the knight and one for the weapon, and the weapon would be parented to the right hand bone so it moves with the animation. There’s nothing really to be gained by using a single mesh for this.
you are absolutely right - there is nothing to gain by using a single mesh for this purpose. So I already have two seperate models (the Knight and a sword) ready to use. But how can I parent the weapon to the right hand bone of my model via Javascript?
One last thing remains - In order to parent the weapon to the left hand bone, I need a reference to that bone. In the editor I see clearly that the armature I used to animate my model is still there in the hierarchy. The remaining question is: how can I get a reference to one particular bone? I assume that it works somehow with “getComponent(…)” but simply putting the name of the bone as a string parameter into “getComponent()” always returns ‘null’…
Thanks guys, I especially like the idea to use a public transform variable for that bone transform (thanks Eric) because that makes it possible to have a designated “Weapon slot” bone independent from the rest of the skeleton, so one could actually use the player script on various models with entirely different bone skeletons as long as the weapon slot bone is set appropriately - great!