Search Unity

IK and Avatar buggy hierarchy dependancy weirdness?

Discussion in 'Animation' started by LaneFox, Apr 20, 2015.

  1. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,519
    I'm trying to do some weapon ik stuff. I have the weapons dropping onto the right hand as a child, then each weapon has an IK target object inside it that the Left Hand should be targetting.

    The IK does something but its like half a meter off and the rotations are incorrect.



    The IK targetting is not accurate at all, but then if I move it out of the hierarchy it becomes totally normal and works fine...



    ... What the heck is happening here?

    The code is super simple and working correctly.

    Code (csharp):
    1.  
    2. public void UpdateIk(GameObject leftHand, GameObject rightHand)
    3.         {
    4.             lh = leftHand;
    5.             rh = rightHand;
    6.         }
    7.  
    8.         void OnAnimatorIK()
    9.         {
    10.             Debug.Log("Doing IK Pass.");
    11.  
    12.             // Pos/Rot Weights
    13.             animator.SetIKPositionWeight(AvatarIKGoal.LeftHand, 1);
    14.             animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 1);
    15.             animator.SetIKRotationWeight(AvatarIKGoal.LeftHand, 1);
    16.             animator.SetIKRotationWeight(AvatarIKGoal.RightHand, 1);
    17.  
    18.             // Left Hand
    19.             animator.SetIKPosition(AvatarIKGoal.LeftHand, lh.transform.position);
    20.             animator.SetIKRotation(AvatarIKGoal.LeftHand, lh.transform.rotation);
    21.  
    22.             // Right Hand
    23.             animator.SetIKRotation(AvatarIKGoal.RightHand, rh.transform.rotation);
    24.             animator.SetIKPosition(AvatarIKGoal.RightHand, rh.transform.position);
    25.         }
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,519
    Would be nice to know if this is a bug or not before I proceed to using another empty gameobject that just mimics the target object's position that Mecanim can point to outside the hierarchy.
     
  3. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Why are you trying to reach the right hand target if the weapon is already a child of the hand?
    Also the left hand final position depend on the right hand final position so you cannot solve this in one IK pass.

    Simply remove all the right hand stuff since the weapon is already a child of the right hand and it should work.

    When you have dependancy between two IK target you can use multiple layer IK pass to resolve this dependancy.
    On the first layer you resolve the right hand IK which will update the right hand transform, then on the second layer you can resolve the left hand IK, but in your case there is no need to use a two step IK since the right hand doesn't need to be solved by IK.
     
  4. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,519
    The hands aren't targeting each other, (the code isn't clearly named, sorry. I'm passing GameObjects as lh/rh targets) but the weapon is indeed parented to the right hand while the left hand is looking at a child of the weapon for an IK target... So I guess this makes a dependency loop. I thought there may be an issue in the hierarchy making a dependancy, but wasn't considering it since I wasn't targeting the RH as the target directly but using a child instead. I can see now that this could be a problem, though.

    First I'll try moving the LH code to after the RH code, then try multiple layers and go from there. I'm working towards a data driven weapon system independent of animations.

    Thanks for looking and checking this out.
     
  5. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    There is no dependancy if the right hand IK is disabled and it should be disabled because the weapon is a child of your right hand.
     
  6. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,519
    I'm using the right hand IK to point the weapon. The animations don't do anything for the arms/hands.
     
  7. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Well in this case you don't have the choice to use two layer
    use your base layer to solve the right hand IK
    create a second empty layer and set IK pass on, use this layer to solve the left hand IK, at this step the right hand transform should be up to date

    Code (CSharp):
    1.  
    2. void OnAnimatorIK(int layerIndex)
    3. {
    4.     // Right Hand
    5.     if(layerIndex == 0)
    6.     {
    7.         animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 1);
    8.         animator.SetIKRotationWeight(AvatarIKGoal.RightHand, 1);
    9.        
    10.         animator.SetIKRotation(AvatarIKGoal.RightHand, rh.transform.rotation);
    11.         animator.SetIKPosition(AvatarIKGoal.RightHand, rh.transform.position);
    12.     }
    13.  
    14.     // Left Hand
    15.     if(layerIndex == 1)
    16.     {
    17.         animator.SetIKPositionWeight(AvatarIKGoal.LeftHand, 1);
    18.         animator.SetIKRotationWeight(AvatarIKGoal.LeftHand, 1);
    19.         animator.SetIKPosition(AvatarIKGoal.LeftHand, lh.transform.position);
    20.         animator.SetIKRotation(AvatarIKGoal.LeftHand, lh.transform.rotation);
    21.     }
    22. }
    23.  
     
    theANMATOR2b and LaneFox like this.
  8. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,519
    Ah, yes, using the second layer was a great idea I didn't know I could use layerIndex like that. I implemented this idea and it works fine, there isn't a dependency issue anymore, the Right Hand can have a goal somewhere, have the weapon attached to it and then the weapon can hold a goal object for the left hand which is making data driven IK weapons idea seem quite feasible.

    Thanks for the support!
     
    Mecanim-Dev likes this.