gIKcontrol.cs : simple Inverse Kinematics for head+hands avatar in Unity3D

Here is the script you will need to attach to your mecAnim, rigged humanoid avatar in order for the Oculus / Hydra head / hands puppeteering solution to function properly:

gIKcontrol.cs


 using UnityEngine;
 using System;
 using System.Collections;
[RequireComponent(typeof(Animator))] 
public class gIKcontrol : MonoBehaviour {
     
     protected Animator animator;
     
     public bool ikActive = false;
     public Transform rightHandObj = null;
     public Transform leftHandObj = null;
     public Transform lookObj = null;
     
     void Start () 
     {
         animator = GetComponent<Animator>();
     }
     
     //a callback for calculating IK
     void OnAnimatorIK()
     {
         if(animator) {
             
             //if the IK is activeset the position and rotation directly to the goal
             if(ikActive) {
                 
                 // HEAD -- Oculus
                 // Set the look target position for the HEADif one has been assigned
                 // the look-at target is an empty GO which extends out from the Oculus CenterEye
                 if(lookObj != null) {
                     animator.SetLookAtWeight(1);
                     animator.SetLookAtPosition(lookObj.position);
                 }    
                // HAND -- Right / Hydra
                 // Set the right hand target position and rotationif one has been assigned
                 if(rightHandObj != null) {
                     animator.SetIKPositionWeight(AvatarIKGoal.RightHand,1);
                     animator.SetIKRotationWeight(AvatarIKGoal.RightHand,1);  
                     animator.SetIKPosition(AvatarIKGoal.RightHand,rightHandObj.position);
                     animator.SetIKRotation(AvatarIKGoal.RightHand,rightHandObj.rotation);
                 }        
                // HAND -- Left / Hydra
                 // Set the LEFT hand target position and rotationif one has been assigned
                 if(leftHandObj != null) {
                     animator.SetIKPositionWeight(AvatarIKGoal.LeftHand,1);
                     animator.SetIKRotationWeight(AvatarIKGoal.LeftHand,1);  
                     animator.SetIKPosition(AvatarIKGoal.LeftHand,leftHandObj.position);
                     animator.SetIKRotation(AvatarIKGoal.LeftHand,leftHandObj.rotation);
                 }        
            }
             
             // if the IK is not activeset the position and rotation 
             // of the hands and head back to the original position
             else {          
                 animator.SetIKPositionWeight(AvatarIKGoal.RightHand,0);
                 animator.SetIKRotationWeight(AvatarIKGoal.RightHand,0); 
                 animator.SetIKPositionWeight(AvatarIKGoal.LeftHand,0);
                 animator.SetIKRotationWeight(AvatarIKGoal.LeftHand,0); 
                 animator.SetLookAtWeight(0);
             }
         }
     }    
 }


			

2 thoughts on “gIKcontrol.cs : simple Inverse Kinematics for head+hands avatar in Unity3D

  1. Pingback: Avatar Puppeteering in VR - dSky/Rift: The VRFaq

  2. Pingback: The Avatar Magna Carta : or, How to Puppeteer a 3D Humanoid with 6DoF head and hands tracking | dSky VR Studios

Comments are closed.