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
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?
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.
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?
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.
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