How to change the AvatarMask at the runtime?

5538592--569674--upload_2020-3-2_0-50-30.png

For some reason i want to change the part of HumanoidBody state at the runtime.

i searched the unity API - 2019.3 and found the “AvatarMask.GetHumanoidBodyPartActive()”

but i dont know how to get this property “AvatarMask” in the Animations module, i have only one “Animator” BTW

is anyone know how to get this property in API?

really need help, thanks!

There’s a SetHumanoidBodyPartActive method too. I’m not sure how you could miss that.

Thanks for the reply, this is my first time to use animator., i found this api " SetHumanoidBodyPartActive" before.
but the problem is i dont know how to get the “AvatarMask” from animations at the runtime.
i try to do something to get this mask, for example animator.avatarmask/ animation.avatarmask??? but it seems like not define in components.

There is no way to get the mask from the Animator like that. You probably just need to have a serialized AvatarMask field in your script and assign the same mask you used in the Animator Controller. If that doesn’t work then it’s probably not possible when using Animator Controllers.

You might also be interested in Animancer (link in my signature) which lets you control everything with scripts at runtime, including modifying masks or assigning different ones.

Hi there
i have an issue with AvatarMask.SetHumanoidBodyPartActive
it only works on Edit Mode but when i build and run the game it doesn’t work
why ?

Hey there,
You can do this:

AnimatorController ac = animator.runtimeAnimatorController as AnimatorController;
AnimatorControllerLayer[] acLayers = ac.layers;
AnimatorControllerLayer acLayer = ac.layers[0]; //Replace 0 with your layer index
AvatarMask mask = acLayer.avatarMask;

Hope this helps!

No you can’t, AnimatorController is an editor-only class.

I don’t know if you figured it out already. But if not and for those who have the same issue, this is how you do it.

first of all, create a variable:

public AvatarMask upperbodyMask; //Call it whatever you want

Then in your function:

upperbodyMask.SetHumanoidBodyPartActive(AvatarMaskBodyPart.Body, true); //use whatever part you want to active or not.

for me let say i want the body part to be active when aiming and not active when not aiming:

public AvatarMask upperbodyMask;

private void PlayerMovement ()
{
   if (isAiming)
       upperbodyMask.SetHumanoidBodyPartActive(AvatarMaskBodyPart.Body, true);
   else
       upperbodyMask.SetHumanoidBodyPartActive(AvatarMaskBodyPart.Body, false);
}