Need Help, DESPERATE! Animating with multiple object pieces

So I am using a character that is composed of multiple pieces.
here’s a picture where you can see the legs are seperate:

Everything works fine when I plop a model in with the rig and all the pieces and an animation. It even works with a seperate female model using the same animation.

The problem lies when switching the model pieces out for other models. I am trying to have the player equip say… a piece of armor in the chest. It would hide the chest model and load in a rigged chestplate armor model.

When I change the mesh of the skinned mesh renderer for the chest, to represent the armor piece it will NOT work. Often the object is just invisible. If I load in an entire character with the armor piece on it, it will work. It will create an additional game object for the armor piece and it works fine. But if I go to the skinned mesh renderer of the chest piece, change the transform position etc to match the armor’s piece, and change the mesh & root bone to match the armor pieces, it will not work. The two skinned mesh renderers are the EXACT SAME as well as the transforms, but if it’s the chest object & not the exact armor game object it will NOT work.

It’s as if there’s data in the skinned mesh renderer that always reflects the original mesh.

How can I get this to work? I have tried everything I can think of. All help is appreciated… thank you! Very desperate.

EDIT: GOT IT TO WORK!

In the code you have to assign the bones to the skinned mesh renderer’s bones variable.
Here’s the code!

        GameObject item = (GameObject) Resources.Load("Items/"+itemID) as GameObject;
        ClientItem cItem = item.GetComponent<ClientItem>();

        GameObject root = GameObject.Find("Character");
        GameObject gameItem = root.transform.Find(""+slot).gameObject;

        SkinnedMeshRenderer skin = gameItem.GetComponent<SkinnedMeshRenderer>();
        SkinnedMeshRenderer referenceSkin = item.GetComponent<SkinnedMeshRenderer>();
        skin.sharedMesh = referenceSkin.sharedMesh;

        Transform[] rootChildren = root.GetComponentsInChildren<Transform>();

        Transform[] bones = new Transform[cItem.bones.Length];
        for(int i = 0; i < bones.Length; i++) {
            foreach(Transform child in rootChildren) {
                if(child.gameObject.name == cItem.bones[i]) {
                    bones[i] = child;
                    break;
                }
            }
        }

        skin.bones = bones;

        skin.sharedMaterials = referenceSkin.sharedMaterials;
        gameItem.transform.position = item.transform.position;

and my ClientItem class I attached to the prefab in my recources folder… It automatically will save the bones onto it.

using UnityEngine;
using System.Collections;
using UnityEditor;

[CustomEditor(typeof(ClientItem))]
public class ClientItemInspector : Editor
{
    public void OnEnable()
    {
        ClientItem myTarget = (ClientItem)target;
        SkinnedMeshRenderer smr = myTarget.gameObject.GetComponent<SkinnedMeshRenderer>();
        if(smr != null && smr.bones.Length > 0 && myTarget.bones == null) {
            string[] bones = new string[smr.bones.Length];
            for(int i = 0; i < smr.bones.Length; i++)
                bones[i] = smr.bones[i].gameObject.name;
            myTarget.bones = bones;
        }
    }
}

public class ClientItem : MonoBehaviour {

       public ClientCharacterHandler.EquipmentSlots slot = ClientCharacterHandler.EquipmentSlots.Weapon;
       public string itemName;
       public string examine;
       public bool twoHanded;
       public bool removeHair;
       public bool removeBeard;
       public string[] bones;

}
1 Like

Bump

You have to rebind the new mesh to the bones. There’s several posts here about it you can find by searching, but I have a script at home I can post later tonight.

Ok. I searched and haven’t found any. I tried using the character customization example code but it didnt work.

Ok. So I fixed it.
I was looking at the character customization example yesterday and understood you have to assign the SkinnedMeshRenderer.bones variable. When you save the skinned mesh renderer as a prefab, it loses the bones variable. So yesterday I had it assign the bones back and I couldn’t get it to work. Today I figured out it wasn’t loading the transforms of the bones correctly and I fixed that, AND NOW IT WORKS!

I shared my code in the 1st post!

1 Like