Search Unity

Creating new clothing for existing character prefab

Discussion in 'Animation' started by Serinx, Sep 18, 2018.

  1. Serinx

    Serinx

    Joined:
    Mar 31, 2014
    Posts:
    788
    I've got a character prefab which has a model using multiple meshes rigged to the same skeleton. This all works great with the clothing that was on the character when I first imported it to Unity.

    I tried to create a new piece of clothing and add it to the blend file in Unity, this works, but the character prefab doesn't get the new piece of clothing automatically (which makes sense).

    I tried to manually copy the mesh from the blend file into the existing prefab but this doesn't seem possible.

    I dragged the blend file into the scene, copied the clothing object to the prefab object in the scene, assigned the root bone to the existing skeleton, but the new clothing does not animate with the existing clothing.

    It all works fine for the blend file if I drag in a fresh copy, but I only want to add the new object.

    I don't want to have to recreate the prefab every time I add a new piece of clothing, because it's got all sorts of scripts and colliders attached to the skeleton.

    Has anyone experienced this before?

    Thanks!
     
  2. Serinx

    Serinx

    Joined:
    Mar 31, 2014
    Posts:
    788
    Found the solution, I wrote a script which sets the skinned mesh renderer bones of the clothing to the bones of the main character and it all went smoothly.
    If anyone else has this issue, just attach the following script to your clothing item and set the Target Skin to your main character mesh object (the one with the working skinned mesh renderer component).


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [ExecuteInEditMode]
    6. public class BoneRetarget : MonoBehaviour {
    7.  
    8.     [SerializeField] SkinnedMeshRenderer targetSkin;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.         SkinnedMeshRenderer mySkin = GetComponent<SkinnedMeshRenderer>();
    13.         mySkin.bones = targetSkin.bones;
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update () {
    18.      
    19.     }
    20. }
     
    Last edited: Sep 20, 2018
  3. comickerlung

    comickerlung

    Joined:
    Jun 11, 2019
    Posts:
    4
    Thanks a lot this skill is very usefully.
     
  4. bobenko

    bobenko

    Joined:
    Nov 8, 2014
    Posts:
    2
    thanks, that helped, setting rootBone only didn't help