Character Clothes not Animating with Character Model
I'm very new to Unity. I'm messing around just trying to get my bearings but I'm having trouble getting a character model to animate with the clothes correctly.
This is the asset that I'm using: https://shawarma.assetstore.unity3d.com/en/#!/content/89723
I've created a ThirdPersonController and dropped my model into that. When I hit play, the base character moves and is animated, but the clothes stay in the default position.
The ReadMe says:
Attaching clothing(Classes): Add the "AttachToBody" script to the clothing, the cloths must be inside the character to work.
I believe I have done this but I could be wrong. I have an error in Unity which says
Wrong parent body
Which I can see in the code for the AttachToBody.cs
if (bodyBones == null)
{
Debug.LogError ("Wrong parent body.");
return;
}
Could somebody help me out and tell me where I'm going wrong please?
I've attached a screenshot for reference.
Answer by Donglecow · Oct 24, 2017 at 02:30 PM
For those interested in the steps I took to fix this, here is what I did based on DMGregory's answer to the same question on Stack Exchange here.
To start with, I created an entirely new character from the same asset.
The hierarchy is:
ThirdPersonController
|-- PlayerModel
|---- CharArmature
|------ CharBaseM
|---- WarriorMale
|------ CharArmature
|-------- etc...
|------ TSM_NonSym
|------ TSM_Sym
|---- Camera
I created a new script to use instead of the included AttachToBody.cs
script, as DMGregory suggested and attached this to ThirdPersonController.PlayerModel.WarriorMale
on the WarriorMale
object. The new script is below.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/**
* This matches the cloth movement to a character, the clothing must be inside the character.
**/
public class AttachToMesh : MonoBehaviour
{
void Start()
{
int i = 0;
Transform rootParent = transform.root.transform;
GameObject target;
Transform[] bodyBones = null;
var skinnedMesh = transform.parent.GetComponentInChildren<SkinnedMeshRenderer>();
if (skinnedMesh != null)
{
bodyBones = skinnedMesh.bones;
}
if (bodyBones == null)
{
Debug.LogError("Wrong parent body.");
return;
}
GameObject Attachment;
for (i = 0; i < transform.GetChildCount(); i++)
{
Attachment = transform.GetChild(i).gameObject;
if (Attachment.GetComponent<SkinnedMeshRenderer>() != null)
{
Attachment.GetComponent<SkinnedMeshRenderer>().bones = bodyBones;
}
}
}
}
Which caused the clothes to animate with the player model. I double checked the original script in this position (and descendants) in the hierarchy and was still unable to get it to animate, but when I used the new AttachToMesh.cs
script, it worked perfectly, as can be seen in the screenshot below of the idle animation of my player model, rather than the default T-Shape pose.
This definitely works, I just tried it. For my case similar deal with two game objects. 1. I had a rig and bare body mesh imported into Unity from Blender. 2. I had a rig and helmet mesh imported into Unity from Blender.
I attached the script to the helmet gameObject (target) and the script worked fine. Be sure to make improvements and adjustments in the script if you feel the need.