- Home /
How to make cloth follow the character movement?
So I created Player character in Blender, add him rig, make it animate. Then i made bracelet in the same blender file, attach it model to the character's skeleton and export like FBX-file. In Unity bracelet prefab parented to the character model. For now bracelet follows player's position when he moves, but not animate with him. So when player moves, his parts animating, but bracelet don't go with forearm. In blender when i move forearm, it's follows properly, so fbx was made fine. I googled this problem and find kinda solution for this. Here's script i found:
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.childCount; i++)
{
Attachment = transform.GetChild(i).gameObject;
if (Attachment.GetComponent<SkinnedMeshRenderer>() != null)
{
Attachment.GetComponent<SkinnedMeshRenderer>().bones = bodyBones;
}
}
}
}
As i understand, this supposed to replace one of the bones of character's skeleton with one, that uses my bracelet prefab. But it didn't work for me as well. No errors, no massages from the script about not finding target bones. Both character mesh (named "Body") and the brcelet model has Skinned mesh attached to it. The script is on bracelet. When script attached to anithing else, bracelet mesh just dissapears or goes inside character's mesh and becomes very small (literally like 0.1 scale) and places in wrong angle. I can't find out where i'm do it wrong. Maybe, it's about poor hierarchy or smth. Please, help, i struggling with this almost whole week. Sorry for bad English :|
Answer by DedWnutry · Apr 26 at 10:03 AM
I finally found the solution by myself somehow! It's very simple, litterally just 2 lines of code. All you need is write in AttachToBody script:
public GameObject target;
void Start()
{
gameObject.GetComponent<SkinnedMeshRenderer>().bones = target.GetComponent<SkinnedMeshRenderer>().bones;
gameObject.GetComponent<SkinnedMeshRenderer>().rootBone = target.GetComponent<SkinnedMeshRenderer>().rootBone;
}
And that's all! This replaces replaces your object rootbone and bones with targer's. Attach this script to the cloth object, parent it to player's model and drag Body mesh in target slot in inspector. Maybe it currently works only in one direction and you can't unequip item, but for now this all i was needed.