Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

AvatarIKGoal not moving to the desired position...

Discussion in 'Animation' started by ellioteserin_dev, May 9, 2020.

  1. ellioteserin_dev

    ellioteserin_dev

    Joined:
    Jan 5, 2020
    Posts:
    23
    So, I have a 3rd person character and when I aim, the character's body can look up and down(to allow for aiming up and down basically) and I want the gun to move up and down accordingly - which it does. I then tried to set up IK so that the hands move to the correct positions on the gun - marked by empty GameObjects.
    The code I am using is:
    Code (CSharp):
    1.         void OnAnimatorIK(int layerIndex)
    2.         {
    3.             if (layerIndex == 0)
    4.             {
    5.                 HandleWeights();
    6.  
    7.                 anim.SetLookAtWeight(weight, bodyWeight, 1, 1, 1);
    8.                 anim.SetLookAtPosition(states.movementValues.aimPosition);
    9.  
    10.                 if (leftHandTarget != null)
    11.                 {
    12.                     UpdateIK(AvatarIKGoal.LeftHand, leftHandTarget, offHandWeight);
    13.                 }
    14.  
    15.                 UpdateIK(AvatarIKGoal.RightHand, rightHandTarget, mainHandWeight);
    16.             }
    17.         }
    18.  
    19.         void UpdateIK(AvatarIKGoal goal, Transform t, float w)
    20.         {
    21.             anim.SetIKPositionWeight(goal, w);
    22.             anim.SetIKRotationWeight(goal, w);
    23.             anim.SetIKPosition(goal, t.position);
    24.             anim.SetIKRotation(goal, t.rotation);
    25.         }
    but, despite all of the variables being correct (the weights are definitely 1 and the transforms arent null) the left arm does not want to go to the leftHandTarget position (especially when the gun is moving up and down). I've read through a lot of forums and can't seem to find an answer that works. If it helps, the animations are humanoid, and all on the base layer (which has IK pass on.) the animations are also baked into pose (to remove any root motion) but I've tried unbaking and it hasn't made a difference.
    I do not directly adjust the hand transforms in my code but I do have an OnAnimatorMove function which sets the shoulder position and rotation.

    I'm just confused as to why it's not working properly. The IK is definitely applying (if I comment it out it resorts back to its original animation pose) but it just doesn't want to follow the target transform.

    Thanks for any help in advance.


    EDIT: On further inspection, the Y position of the target transform doesn't seem to change, even though I can see it changing in the scene view. When I debug the value, it stays at about 1.4 even though the Y position in the scene is moving by a lot. When I move the position along the Y axis in the inspector, it does work however.
     
    Last edited: May 9, 2020
unityunity