[TUTORIAL] How to make clothes animate along with character

Hello!
Many want to have different clothes for their game that you can swap with the character. There are a few approchaes to this. One is to have different body parts with different clothes attached to it and swap the whole parts. This is kindoff a dirt method in my oppinion but works quite well.

The other way to do it is to rig the clothing in question and make it’s bones follow the bones of the character. That’s the method I will show how to do.
Firstly, let me show you some results from my own game.
Here is my temporary customization window.

And here is the ingame result:

Alright, so now for how to do it. Firstly what we have done is to use the same rig for the player and for all the clothing that needs rigging. Thing’s like glasses can just be a child of the head. Then I use a script that sync the bones up. I am not the creator of the script, and I sadly can’t remember where I found it. I think it was somehwere on the Unity Answers. If anyone know where. Please let me know. But all you have to do is replace the clothing’s animator component (Which get’s added automatically) with the following script.

Edit: Credits to Pixels for originally posting the script:

using System.Collections.Generic;
using UnityEngine;

public class Equipmentizer : MonoBehaviour
{
    public SkinnedMeshRenderer TargetMeshRenderer;

    void Start()
    {
        Dictionary<string, Transform> boneMap = new Dictionary<string, Transform>();
        foreach (Transform bone in TargetMeshRenderer.bones)
            boneMap[bone.gameObject.name] = bone;


        SkinnedMeshRenderer myRenderer = gameObject.GetComponent<SkinnedMeshRenderer>();

        Transform[] newBones = new Transform[myRenderer.bones.Length];
        for (int i = 0; i < myRenderer.bones.Length; ++i)
        {
            GameObject bone = myRenderer.bones[i].gameObject;
            if (!boneMap.TryGetValue(bone.name, out newBones[i]))
            {
                Debug.Log("Unable to map bone \"" + bone.name + "\" to target skeleton.");
                break;
            }
        }
        myRenderer.bones = newBones;

    }
}

Then you drag the characters skinned mesh renderer into the “TargetMeshRenderer” field. And it should all be working. Hope this helped

18 Likes

Thanks for sharing!

Do you get much poke-through of the original model through the clothing mesh? Do you ever turn off any of the base model’s mesh to prevent poke-through, such as turning off the bare leg mesh when the character is wearing pants that completely cover the legs?

No, the character is one big mesh. And the poking is not noticable at all from my experiance.

Thanks!

How to do that step with "same rigging with character and clothes?

1 Like

Just use the exact same skeleton when you rig. If you notice clipping you need to weight paint to fix it.

Okay… but when I made my character in character creator v2 (iclone) … the rigging was like automatic… didnt even notice it… but then I have one more character with exact same rig but with different shoes, can I use them? I dont really understand this…

That is really for your software you create the rigs in. I can’t help you with that. All I am showing in this article is how to exchange bones. Basically make your clothes bones use the character bones so they match up.

1 Like

Thank you. Just how to disable it when we need to take cloth off? It works good but I need to revert cloth back to its position again.

Remap the bones back to the original gameobjects?

1 Like

Do you know what’s the difference between the script on first post and below script?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ClothEquipmentizer : MonoBehaviour { public GameObject target;

    // Use this for initialization
    void Start () {
        SkinnedMeshRenderer targetRenderer = target.GetComponent<SkinnedMeshRenderer>();
        Dictionary<string, Transform> boneMap = new Dictionary<string, Transform>();
        foreach( Transform bone in targetRenderer.bones )
        {
            boneMap[bone.name] = bone;
        }

        SkinnedMeshRenderer thisRenderer = GetComponent<SkinnedMeshRenderer>();
        Transform[] boneArray = thisRenderer.bones;
        for(int idx = 0; idx < boneArray.Length; ++idx )
        {
            string boneName = boneArray[idx].name;
            if( false == boneMap.TryGetValue(boneName, out boneArray[idx]) )
            {
                Debug.LogError("failed to get bone: " + boneName);
                Debug.Break();
            }
        }
        thisRenderer.bones = boneArray; //take effect
      
    }
  
    // Update is called once per frame
    void Update () {
      
    }
}

Which is better to use?

If I understand correctly, this leads to several skinned meshes per character. This of course slows the game down. I know there are ways to merge them, however I have yet to succeed. Have you tried this, and if so, any success?

Not too informed on meshes. But I think a skinned mesh just contains a mesh, you should be able to simply generate the mesh. Not too sure.

Can you give me an example on how to implement this? Where do I attach it? Do I need exactly every bone or just the bones near the mesh and stuff? Can you give me a picture of the hiearchy?

You could probably get away with skipping bones. If you manually map x bones to y bones. But that might cause issues with weighting and might not map very well. All we are doing is to replace the clothing bones with the bones of the rig. As you can see in the example, very simple to do.

As for example how to implement? I gave one in the original post that is written by Pixels

maybe can you give me a screenshot in the inspector and in the hiearchy?

I haven’t tried it yet, Not even copy pasted the code.
Before I do, Do you put the script on each bone you want to emulate? or Just put it somewhere and assign the bones to emulate.

I can’t give you a hierarchy screenshot right now. It’s just the bones anyways. No biggie.

And check line 15

Not working for me. Do you apply the script to the parent or the Mesh?

the one with the renderer

idk what happened, ive had this working before but now whenever i set this up it seems to warp my mesh inside the character model and take position where the bones are

heres image

the highlighted part is what is supposed to be the players pants, but ye it goes inside the body and then turns into the bones