Search Unity

Need help with IK animation

Discussion in 'Animation' started by MikeyJY, Feb 1, 2020.

  1. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    I'm trying to lear Inverse Kinematics animation, I'm searching for a good tutorial to learn, but I didn't find one for starters! Can you explain me basics of IK unity system! I found on ScriptingAPI page this example:
    Code (CSharp):
    1. using UnityEngine;
    2. using System;
    3. using System.Collections;
    4.  
    5. [RequireComponent(typeof(Animator))]
    6.  
    7. public class IKControl : MonoBehaviour {
    8.    
    9.     protected Animator animator;
    10.    
    11.     public bool ikActive = false;
    12.     public Transform rightHandObj = null;
    13.     public Transform lookObj = null;
    14.  
    15.     void Start ()
    16.     {
    17.         animator = GetComponent<Animator>();
    18.     }
    19.    
    20.     //a callback for calculating IK
    21.     void OnAnimatorIK()
    22.     {
    23.         if(animator) {
    24.            
    25.             //if the IK is active, set the position and rotation directly to the goal.
    26.             if(ikActive) {
    27.  
    28.                 // Set the look __target position__, if one has been assigned
    29.                 if(lookObj != null) {
    30.                     animator.SetLookAtWeight(1);
    31.                     animator.SetLookAtPosition(lookObj.position);
    32.                 }  
    33.  
    34.                 // Set the right hand target position and rotation, if one has been assigned
    35.                 if(rightHandObj != null) {
    36.                     animator.SetIKPositionWeight(AvatarIKGoal.RightHand,1);
    37.                     animator.SetIKRotationWeight(AvatarIKGoal.RightHand,1);
    38.                     animator.SetIKPosition(AvatarIKGoal.RightHand,rightHandObj.position);
    39.                     animator.SetIKRotation(AvatarIKGoal.RightHand,rightHandObj.rotation);
    40.                 }      
    41.                
    42.             }
    43.            
    44.             //if the IK is not active, set the position and rotation of the hand and head back to the original position
    45.             else {        
    46.                 animator.SetIKPositionWeight(AvatarIKGoal.RightHand,0);
    47.                 animator.SetIKRotationWeight(AvatarIKGoal.RightHand,0);
    48.                 animator.SetLookAtWeight(0);
    49.             }
    50.         }
    51.     }  
    52. }
    53.  
    It worked for ThirdPersonCharacter from Standard Assets but I don't know how to extend it to more complex skeleton because AvatarIKGoal has only 4 proprieties and SetLookAtWeight for head position! But The character has thumbs, palms, shoulders, etc. And I don't know how to animate them!