Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  5. Nominations have been announced for this years Unity Awards. Celebrate the wonderful projects made by your peers this year and get voting! Vote here!
    Dismiss Notice
  6. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

[TUTORIAL] How to make clothes animate along with character

Discussion in 'Animation' started by TwoTen, Jun 8, 2017.

  1. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,011
    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.
    https://i.gyazo.com/81c191197374c0667f93bbc65dd31bd5.mp4
    And here is the ingame result:
    https://i.gyazo.com/13b391865b3b658658b3dacaac690875.mp4


    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:
    http://answers.unity3d.com/questions/44355/shared-skeleton-and-animation-state.html

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3.  
    4. public class Equipmentizer : MonoBehaviour
    5. {
    6.     public SkinnedMeshRenderer TargetMeshRenderer;
    7.  
    8.     void Start()
    9.     {
    10.         Dictionary<string, Transform> boneMap = new Dictionary<string, Transform>();
    11.         foreach (Transform bone in TargetMeshRenderer.bones)
    12.             boneMap[bone.gameObject.name] = bone;
    13.  
    14.  
    15.         SkinnedMeshRenderer myRenderer = gameObject.GetComponent<SkinnedMeshRenderer>();
    16.  
    17.         Transform[] newBones = new Transform[myRenderer.bones.Length];
    18.         for (int i = 0; i < myRenderer.bones.Length; ++i)
    19.         {
    20.             GameObject bone = myRenderer.bones[i].gameObject;
    21.             if (!boneMap.TryGetValue(bone.name, out newBones[i]))
    22.             {
    23.                 Debug.Log("Unable to map bone \"" + bone.name + "\" to target skeleton.");
    24.                 break;
    25.             }
    26.         }
    27.         myRenderer.bones = newBones;
    28.  
    29.     }
    30. }
    Then you drag the characters skinned mesh renderer into the "TargetMeshRenderer" field. And it should all be working. Hope this helped
     
    Last edited: Jun 9, 2017
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    8,628
    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?
     
  3. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,011
    No, the character is one big mesh. And the poking is not noticable at all from my experiance.
     
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    8,628
    Thanks!
     
  5. mattis89

    mattis89

    Joined:
    Jan 10, 2017
    Posts:
    932
    How to do that step with "same rigging with character and clothes?
     
  6. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,011
    Just use the exact same skeleton when you rig. If you notice clipping you need to weight paint to fix it.
     
  7. mattis89

    mattis89

    Joined:
    Jan 10, 2017
    Posts:
    932
    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...
     
  8. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,011
    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.
     
    mattis89 likes this.
  9. ksam2

    ksam2

    Joined:
    Apr 28, 2012
    Posts:
    966
    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.
     
    Last edited: Jan 29, 2018
  10. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,011
    Remap the bones back to the original gameobjects?
     
    ksam2 likes this.
  11. ksam2

    ksam2

    Joined:
    Apr 28, 2012
    Posts:
    966
    Do you know what's the difference between the script on first post and below script?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ClothEquipmentizer : MonoBehaviour { public GameObject target;
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.         SkinnedMeshRenderer targetRenderer = target.GetComponent<SkinnedMeshRenderer>();
    10.         Dictionary<string, Transform> boneMap = new Dictionary<string, Transform>();
    11.         foreach( Transform bone in targetRenderer.bones )
    12.         {
    13.             boneMap[bone.name] = bone;
    14.         }
    15.  
    16.         SkinnedMeshRenderer thisRenderer = GetComponent<SkinnedMeshRenderer>();
    17.         Transform[] boneArray = thisRenderer.bones;
    18.         for(int idx = 0; idx < boneArray.Length; ++idx )
    19.         {
    20.             string boneName = boneArray[idx].name;
    21.             if( false == boneMap.TryGetValue(boneName, out boneArray[idx]) )
    22.             {
    23.                 Debug.LogError("failed to get bone: " + boneName);
    24.                 Debug.Break();
    25.             }
    26.         }
    27.         thisRenderer.bones = boneArray; //take effect
    28.      
    29.     }
    30.  
    31.     // Update is called once per frame
    32.     void Update () {
    33.      
    34.     }
    35. }
    36.  
    Which is better to use?
     
  12. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    334
    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?
     
  13. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,011
    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.
     
  14. JemoYT

    JemoYT

    Joined:
    Feb 13, 2017
    Posts:
    7
    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?
     
  15. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,011
    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
     
  16. JemoYT

    JemoYT

    Joined:
    Feb 13, 2017
    Posts:
    7
    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.
     
    Last edited: Jun 29, 2018
  17. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,011
    I can't give you a hierarchy screenshot right now. It's just the bones anyways. No biggie.

    And check line 15
     
  18. Liamkirt

    Liamkirt

    Joined:
    Jun 6, 2017
    Posts:
    1
    Not working for me. Do you apply the script to the parent or the Mesh?
     
  19. mattis89

    mattis89

    Joined:
    Jan 10, 2017
    Posts:
    932
    the one with the renderer
     
  20. s2celerity

    s2celerity

    Joined:
    Nov 20, 2018
    Posts:
    2
    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

    https://imgur.com/a/AZ2Kz6h

    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
     
  21. s2celerity

    s2celerity

    Joined:
    Nov 20, 2018
    Posts:
    2

    after several hours (more like all day) of figuring things out,
    the problem was that the mesh scale wasnt set properly and after turning off the import scale i adjusted it and now it works great
     
  22. PJRM

    PJRM

    Joined:
    Mar 4, 2013
    Posts:
    280
    @TwoTen
    I like the idea. I was struggling to figure it out how i would do it. however i'll try to know other methods too. May have another way of doing it automatically.