Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to change the AvatarMask at the runtime?

Discussion in 'Animation' started by qiufeng8866, Mar 1, 2020.

  1. qiufeng8866

    qiufeng8866

    Joined:
    Apr 18, 2015
    Posts:
    3
    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!
     
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,553
  3. qiufeng8866

    qiufeng8866

    Joined:
    Apr 18, 2015
    Posts:
    3
    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.
     
  4. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,553
    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.
     
  5. KhalilAKM

    KhalilAKM

    Joined:
    Oct 19, 2019
    Posts:
    2
    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 ?
     
  6. Vellyxenya

    Vellyxenya

    Joined:
    Jan 21, 2021
    Posts:
    1
    Hey there,
    You can do this:

    Code (CSharp):
    1. AnimatorController ac = animator.runtimeAnimatorController as AnimatorController;
    2. AnimatorControllerLayer[] acLayers = ac.layers;
    3. AnimatorControllerLayer acLayer = ac.layers[0]; //Replace 0 with your layer index
    4. AvatarMask mask = acLayer.avatarMask;
    Hope this helps!
     
  7. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,553
    No you can't, AnimatorController is an editor-only class.