Search Unity

Mecanim IK Hands / Head Problem (Aiming Solution)

Discussion in 'Scripting' started by MarshianMan, Jan 31, 2016.

  1. MarshianMan

    MarshianMan

    Joined:
    Mar 27, 2015
    Posts:
    50
    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:

    Code (JavaScript):
    1.  
    2. //The objects are the player hand IK targets
    3. var object : Transform;
    4. var object2 : Transform;
    5. var animator : Animator;
    6.  
    7. //This is where the player's target is stored
    8. var plma : PlayerManager;
    9.  
    10. function OnAnimatorIK () {
    11.  
    12. //This part of the code means if the player is aiming using the right mouse button, the head focuses on the target
    13.     if(animator.GetBool("Focus")){
    14.    
    15.         animator.SetLookAtPosition (plma.target.position);
    16.         animator.SetLookAtWeight(1);
    17.     }
    18.  
    19.  
    20.     animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 1);
    21.     animator.SetIKRotationWeight(AvatarIKGoal.RightHand, 1);
    22.     animator.SetIKPositionWeight(AvatarIKGoal.LeftHand, 1);
    23.     animator.SetIKRotationWeight(AvatarIKGoal.LeftHand, 1);
    24.  
    25.     animator.SetIKPosition(AvatarIKGoal.LeftHand, object2.position);
    26.     animator.SetIKRotation(AvatarIKGoal.LeftHand, object2.rotation);
    27.  
    28.     animator.SetIKPosition(AvatarIKGoal.RightHand, object.position);
    29.     animator.SetIKRotation(AvatarIKGoal.RightHand, object.rotation);
    30.  
    31. }
    (See below for clarifications.)

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

    Hands in correct position when player is aiming straight:
    upload_2016-1-31_9-54-46.png

    When trying to aim vertically:
    upload_2016-1-31_9-56-23.png

    Thanks,
     

    Attached Files:

  2. MarshianMan

    MarshianMan

    Joined:
    Mar 27, 2015
    Posts:
    50
    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.

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var leftHandTarget: Transform;
    4. var rightHandTarget: Transform;
    5. var animator : Animator;
    6.  
    7. var target : Transform;
    8.  
    9. function Start () {
    10.  
    11. }
    12.  
    13. function OnAnimatorIK (layerIndex : int) {
    14.  
    15.     if(layerIndex == 0)
    16.     {
    17.         if(target != null)
    18.         {
    19.             animator.SetLookAtPosition (target.position);
    20.             animator.SetLookAtWeight(1, 1 ,1);
    21.         }
    22.     }
    23.  
    24.     if(layerIndex == 1)
    25.     {
    26.         animator.SetIKPosition(AvatarIKGoal.LeftHand, leftHandTarget.position);
    27.         animator.SetIKRotation(AvatarIKGoal.LeftHand, leftHandTarget.rotation);
    28.  
    29.         animator.SetIKPosition(AvatarIKGoal.RightHand, rightHandTarget.position);
    30.         animator.SetIKRotation(AvatarIKGoal.RightHand, rightHandTarget.rotation);
    31.  
    32.         animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 1);
    33.         animator.SetIKRotationWeight(AvatarIKGoal.RightHand, 1);
    34.         animator.SetIKPositionWeight(AvatarIKGoal.LeftHand, 1);
    35.         animator.SetIKRotationWeight(AvatarIKGoal.LeftHand, 1);
    36.     }
    37.  
    38. }
     
    knopkem and CassioKF like this.
  3. cworld

    cworld

    Joined:
    Feb 6, 2020
    Posts:
    8
    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