0
\$\begingroup\$

How do you have rigged clothing animate with a base model?

Or how can you coalesce multiple skinned mesh renderers or have them all be controlled by just one?

Or, do you just have multiple animators for each SkinnedMeshRenderer (and there would be quite a number, for a character with a complex outfit that is modular)?


Have not been able to get it to work from the only thing I can find online (surprised there aren't more tutorials on this) https://forum.unity.com/threads/tutorial-how-to-make-clothes-animate-along-with-character.475253/

I've applied the method by looping through the SkinnedMeshRenderers present in the clothing assigned when a character root is loaded in ApplyClothing(clothing,root). Clothing and character have the same bone structure, as the clothing is custom modeled to the character model and rig.

I get a few unable to map L/R_Knee Ankle Foot errors, but I have removed the break. I've tried rotating several other joints in the root character but the clothing is still not deforming with the joints.

    void ApplyClothing(GameObject clothing,Transform root)
    {
        GameObject g = Instantiate(clothing);
        g.transform.parent = root;
        g.transform.localPosition = Vector3.zero;

        BoneSyncer(clothing, root);
    }

    void BoneSyncer(GameObject clothing,Transform root)
    {
        SkinnedMeshRenderer[] smrclothing = clothing.GetComponentsInChildren<SkinnedMeshRenderer>();

        for(int i = 0; i < smrclothing.Length; i++)
        {
            SkinnedMeshRenderer[] rootsmrs = root.GetComponentsInChildren<SkinnedMeshRenderer>();
            SkinnedMeshRenderer rootsmr = null;
            for(int j = 0; j < rootsmrs.Length; j++)
            {
                if (!rootsmrs[i].name.ToLower().Contains("haircut"))
                {
                    rootsmr = rootsmrs[i];
                }
            }

           if(rootsmr!=null) BoneSyncer(smrclothing[i], rootsmr);
        } 
    }

    void BoneSyncer(SkinnedMeshRenderer clothingmeshrenderer, SkinnedMeshRenderer targetmeshrenderer)
    {
        Dictionary<string, Transform> boneMap = new Dictionary<string, Transform>();
        foreach (Transform bone in targetmeshrenderer.bones)
            boneMap[bone.gameObject.name] = bone;
  
        Transform[] newBones = new Transform[clothingmeshrenderer.bones.Length];
        for (int i = 0; i < clothingmeshrenderer.bones.Length; ++i)
        {
            GameObject bone = clothingmeshrenderer.bones[i].gameObject;
            if (!boneMap.TryGetValue(bone.name, out newBones[i]))
            {
                Debug.Log("Unable to map bone \"" + bone.name + "\" to target skeleton.");
                //break;
            }
        }
        clothingmeshrenderer.bones = newBones;
    }

```
\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.