Mecanim IK Hands / Head Problem (Aiming Solution)

I’m having some trouble with the Mecanim IK Hands following a weapon that is attached to the head of the player. Going horizontally works fine, but when I use the head to aim vertically (using the Mecanim SetLookAt) the hands do not follow the IK objects attached to the weapon.

Extra note: I do have the current layer IK Pass on.

Here’s the code I’m using:

//The objects are the player hand IK targets
var object : Transform;
var object2 : Transform;
var animator : Animator;

//This is where the player's target is stored
var plma : PlayerManager;

function OnAnimatorIK () {

//This part of the code means if the player is aiming using the right mouse button, the head focuses on the target
    if(animator.GetBool("Focus")){
   
        animator.SetLookAtPosition (plma.target.position);
        animator.SetLookAtWeight(1);
    }


    animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 1);
    animator.SetIKRotationWeight(AvatarIKGoal.RightHand, 1);
    animator.SetIKPositionWeight(AvatarIKGoal.LeftHand, 1);
    animator.SetIKRotationWeight(AvatarIKGoal.LeftHand, 1);

    animator.SetIKPosition(AvatarIKGoal.LeftHand, object2.position);
    animator.SetIKRotation(AvatarIKGoal.LeftHand, object2.rotation);

    animator.SetIKPosition(AvatarIKGoal.RightHand, object.position);
    animator.SetIKRotation(AvatarIKGoal.RightHand, object.rotation);

}

(See below for clarifications.)

-Object is set to Cylinder 1, Object 2 is Cylinder 2

Hands in correct position when player is aiming straight:

When trying to aim vertically:

Thanks,

After some work, I’ve answered my own question.

Apparently you are supposed to pass the Hand IKs on a layer above the IK that the Set Look At is being ran through.

Here is my solution code, for anyone else who runs through this problem. I’ve modified it to fit anyone’s basic needs.

#pragma strict

var leftHandTarget: Transform;
var rightHandTarget: Transform;
var animator : Animator;

var target : Transform;

function Start () {

}

function OnAnimatorIK (layerIndex : int) {

    if(layerIndex == 0)
    {
        if(target != null)
        {
            animator.SetLookAtPosition (target.position);
            animator.SetLookAtWeight(1, 1 ,1); 
        }
    }

    if(layerIndex == 1)
    {
        animator.SetIKPosition(AvatarIKGoal.LeftHand, leftHandTarget.position);
        animator.SetIKRotation(AvatarIKGoal.LeftHand, leftHandTarget.rotation);

        animator.SetIKPosition(AvatarIKGoal.RightHand, rightHandTarget.position);
        animator.SetIKRotation(AvatarIKGoal.RightHand, rightHandTarget.rotation);

        animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 1);
        animator.SetIKRotationWeight(AvatarIKGoal.RightHand, 1);
        animator.SetIKPositionWeight(AvatarIKGoal.LeftHand, 1);
        animator.SetIKRotationWeight(AvatarIKGoal.LeftHand, 1);
    }

}
2 Likes

i have the same question,and your solution works,thanks.but why we need 2animtor layers?is there any better solution,aftaer all ,the second layer is more complex