Replace / Modify Humanoid Avatar during Runtime
Hello everyone,
I am trying to build a new Humanoid Avatar and replace my current Humanoid Avatar during runtime.
The idea behind this is that I need to change the proportions of the rig dynamically (e.g. change the arm length or the width of the shoulders etc.). I wrote a test script where I only change the length of the left arm. Without changing the Avatar this change is immediately overwritten by the Animator as soon as I play an Animation. Thats when I figured I also have to create a new Avatar with the new skeleton proportions.
Itermediate Solution: The creation of a new Avatar works fine as long as I add a new Animator component before setting my newly created Avatar. This means I start without an Animator component attached to my character and then add a fresh Animator component and set my new Avatar as well as an AnimationController (first branch in the code).
Remaining Problem: If I want to just replace the current avatar of the existing Animator the changes are ignored (else branch in the code). The avatar is replaced (according to the name in the Inspector) but the proportions of the skeleton remain the same. I also tried to destroy the Animator Component before adding a new one (as stated above the whole problem only occures with updating an existing Animator), but this does not really work as Unity's Destroy function is delayed.
This is my current code (only showing the relevant parts IHMO):
//Update...
//... modify bone proportions (e.g. arm length)
Animator anim = gameObject.GetComponent<Animator>();
if (!anim)
{
//[!!] if this branch is executed, everything works fine
anim = gameObject.AddComponent<Animator>();
anim.runtimeAnimatorController = animationController;
}
else
{
//[!!] if this branch is executed, the new avatar is ignored
Destroy(anim.avatar);
anim.avatar = null;
}
//snapshot gameObject's parent and pose
Vector3 originalPositon = transform.localPosition;
Quaternion originalRotation = transform.localRotation;
Vector3 originalScale = transform.localScale;
Transform parent = transform.parent;
//detach gameObject and place it into the world origin
transform.SetParent(null, false);
transform.localPosition = Vector3.zero;
transform.localRotation = Quaternion.identity;
transform.localScale = Vector3.one;
//custom method to set the skeleton to T-Pose
SetToTPose(gameObject);
//custom method to create the human description
HumanDescription description = BuildHumanDescription(gameObject);
Avatar avatar = AvatarBuilder.BuildHumanAvatar(gameObject, description);
if(avatar.isValid)
{
anim.avatar = avatar;
}
else
{
Debug.LogError("Avatar Invalid!");
}
//reset gameObject's parent and pose
transform.SetParent(parent, false);
transform.localPosition = originalPositon;
transform.localRotation = originalRotation;
transform.localScale = originalScale;
Your answer
Follow this Question
Related Questions
Humanoid Animations 0 Answers
Using an Avatar locks Movement in Character When Animating 0 Answers
Mixamo animations get weird when set to humanoid 2 Answers
Humanoid Avatar Muscle Pose Issue 1 Answer