Search Unity

How to IK animate with Avatar Masks

Discussion in 'Animation' started by MikeyJY, Jan 31, 2020.

  1. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    Im trying to learn IK animations. On the unity scriptingAPI page I found this example and I tested on the ThirdPersonCharacter from Standard Assets:
    Code (CSharp):
    1. using UnityEngine;
    2. using System;
    3. using System.Collections;
    4.  
    5. [RequireComponent(typeof(Animator))]
    6.  
    7. public class IK : MonoBehaviour
    8. {
    9.  
    10.     protected Animator animator;
    11.  
    12.     public bool ikActive = false;
    13.     public Transform rightHandObj = null;
    14.     public Transform lookObj = null;
    15.  
    16.     void Start()
    17.     {
    18.         animator = GetComponent<Animator>();
    19.     }
    20.  
    21.     //a callback for calculating IK
    22.     void OnAnimatorIK()
    23.     {
    24.         if (animator)
    25.         {
    26.  
    27.             //if the IK is active, set the position and rotation directly to the goal.
    28.             if (ikActive)
    29.             {
    30.  
    31.                 // Set the look __target position__, if one has been assigned
    32.                 if (lookObj != null)
    33.                 {
    34.                  
    35.                     animator.SetLookAtWeight(1);
    36.                     animator.SetLookAtPosition(lookObj.position);
    37.                 }
    38.  
    39.                 // Set the right hand target position and rotation, if one has been assigned
    40.                 if (rightHandObj != null)
    41.                 {
    42.                  
    43.                    
    44.                  
    45.                     animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 1);
    46.                     animator.SetIKRotationWeight(AvatarIKGoal.RightHand, 1);
    47.                     animator.SetIKPosition(AvatarIKGoal.RightHand, rightHandObj.position);
    48.                     animator.SetIKRotation(AvatarIKGoal.RightHand, rightHandObj.rotation);
    49.                 }
    50.  
    51.             }
    52.  
    53.             //if the IK is not active, set the position and rotation of the hand and head back to the original position
    54.             else
    55.             {
    56.                 animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 0);
    57.                 animator.SetIKRotationWeight(AvatarIKGoal.RightHand, 0);
    58.                 animator.SetLookAtWeight(0);
    59.             }
    60.         }
    61.     }
    62. }
    63.  
    I set the rightHandObj a moving cylinder. I noticed that the character is moving the right arm but the palm is frozen:
    upload_2020-1-31_20-10-44.png

    The AvatarIKGoal has only 4 proprieties: Right/Left Hand/Foot. I guess I need an humanoid Avatar Mask to control more body parts(like thumbs, shoulders, head, etc.)! I found this enumeration: AvatarMaskBodyPart that has more humanoid bones! The problem is that SetIKPositionWeight doesn't accept it as a parameter and I don't know how to use this!

    EDIT: Sorry if I don't reply to your answer because I will be AFK some hours!
     
    Last edited: Jan 31, 2020
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    Avatar Masks are for preventing animations from affecting certain body parts, but they have nothing to do with IK. The inbuilt IK system only supports hands and feet (and head look at).

    The Animation Rigging package is a lot more flexible and can probably do what you want, though I've never really used it so I'm not sure what you would need to do.
     
  3. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    Ok, Thanks but I need something more certain!