- Home /
Bones do not switch when called for (Find bones of player then replace clothes bones)
Hello! I have an issue with my script where as the bones that i assign and the bones that i get do not replace.
Basically my script is designed to replace the bones inside of specific clothing, in order to fit on my character. So the script gets all the bones from the skinned-meshrenderer of the clothe, finds all the bones inside of the player and then attempts to replace the bones assigned to the clothe with the playerbones, thus making the cloth act upon the player-bones. My issue is that after the all the bones have been found (both inside the clothe and the player), none of the bones are replaced. I'm not too sure what I am doing wrong as I have tested a previous script where this worked almost perfectly. My issue with the previous script is that the clothing that I want to use, uses multiple bones from my character. That script only let you assing one skinnedmeshrenderer (in my case, I have multiple skinnedmeshrenderers for my characters body), therefore making it not find the correct bones or leaving it with missing bones.
Here is my code:
void Start()
{
GameObject playerBones = GameObject.Find("ThirdPersonCharacter/Male/Male"); // Find the Parent of the bones
Transform[] allChildren = playerBones.GetComponentsInChildren<Transform>(); // Get a list of all the children from the parent
print("Object: " + playerBones); // Displays which gameobject the script found
Dictionary<string, Transform> playerboneMap = new Dictionary<string, Transform>(); //Creates a dictionary for the bones
foreach (Transform child in allChildren)
{
playerboneMap[child.name] = child;
}
SkinnedMeshRenderer thisRenderer = GetComponent<SkinnedMeshRenderer>();
Transform[] boneArray = thisRenderer.bones;
for (int idx = 0; idx < boneArray.Length; ++idx) //this checks if the bones match up, debugs an error if a bone is missing
{
string boneName = boneArray[idx].name;
if (false == playerboneMap.TryGetValue(boneName, out boneArray[idx]))
{
Debug.LogError("failed to get bone: " + boneName);
Debug.Break();
}
}
thisRenderer.bones = boneArray; //take effect and replace the previous bones with the playerbones
// Debugging, this will display all the children of the parent (the bones of the player)
foreach (KeyValuePair<string, Transform> bone in playerboneMap)
{
print("Bone: " + bone.Key);
}
}
I really hope someone can help me out with this issue, as it will greatly increase my work on this game. :)
Thanks in advance!
Answer by saiverx · May 01, 2018 at 01:14 AM
Something to add is that I have looked around on this forum for some similar results, yet no post has solved this issue. My code is based of one of the answers from this post
Yet I do believe that the error resolves around the thisRenderer.bones = boneArray; //take effect and replace the previous bones with the playerbones
-part. But I'm not sure how I can assign the bones in any other way?
Do all the clothing mesh's bones have a weight associated with them, even if it's 0? Bones with no weight (not 0, but none at all) will not be inclded when you export your mesh. This issue is detailed in the link you posted.
Hi, Cherno! Yes my bones do have a weight associated with them (both my playermodel and the clothing mesh). I am using the exact same rig/bones from my player on the clothes. So in theory there should not be a problem replacing the existing bones in the clothing mesh with the player-bones. Thank you for your suggestion! I will look at bit further into this and see if maybe I have done something wrong.
::EDIT:: https://imgur.com/a/DY61Qvq Here you can see both the models and rigs in 3ds max, with bone-weights.
Your answer
Follow this Question
Related Questions
How to get the initial bone positions for a skinned mesh 0 Answers
What space are the bones from an armature and the vertices of a mesh in? 1 Answer
Is it possible to update a skinned mesh renderer's bones? 1 Answer
How can I get the original bone bind position ( and not the matrix)? 1 Answer
Transfer bones from one Object to another (same meshes) 0 Answers