Search Unity

SetIKRotation doesn't work as expected

Discussion in 'Scripting' started by samson96, Aug 11, 2022.

  1. samson96

    samson96

    Joined:
    Jan 8, 2020
    Posts:
    6
    Hello everyone. I've recently started playing around with built-in IK, so here's the script:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class ProceduralLayer : MonoBehaviour
    4. {
    5.     public Transform rightHandTarget;
    6.     public Transform leftHandTarget;
    7.  
    8.     private Animator _animator;
    9.  
    10.     private Quaternion _rot;
    11.  
    12.     private void Start()
    13.     {
    14.         _animator = GetComponent<Animator>();
    15.     }
    16.  
    17.     private void OnAnimatorIK(int layerIndex)
    18.     {
    19.         _animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 1f);
    20.         _animator.SetIKPositionWeight(AvatarIKGoal.LeftHand, 1f);
    21.  
    22.         _rot = _animator.GetBoneTransform(HumanBodyBones.RightHand).rotation;
    23.  
    24.         _animator.SetIKPosition(AvatarIKGoal.RightHand, rightHandTarget.position);
    25.         _animator.SetIKPosition(AvatarIKGoal.LeftHand, leftHandTarget.position);
    26.      
    27.         _animator.SetIKRotationWeight(AvatarIKGoal.RightHand, 1f);
    28.         _animator.SetIKRotation(AvatarIKGoal.RightHand, _rot);
    29.     }
    30. }
    What I want to do is to change the IK position only, right hand rotation must remain the same as the animation pose.

    It shouldn't change the rotation of the right hand, but it does for some reason. According to the documentation, SetIKRotation sets rotation in world space. _rot is a world space rotation, why then the final result is different? I must've missed something, but I have no idea why. Any help is appreciated.

    That's what I get:


    How it should look:

     
  2. samson96

    samson96

    Joined:
    Jan 8, 2020
    Posts:
    6
    Here are the images
     

    Attached Files:

  3. YondernautsGames

    YondernautsGames

    Joined:
    Nov 24, 2014
    Posts:
    354
    Did you ever get to the bottom of this? I'm seeing the same effect
     
  4. samson96

    samson96

    Joined:
    Jan 8, 2020
    Posts:
    6
    Wow, one year and no responses). So the main idea is that humanoids have offsets for IK, yes, there's nothing about it in the docs. You can calculate those offsets in Start() if I recall correctly. There was a thread with a Unity developer comment on this "by design" problem, but after one year I hardly imagine finding it, so apologies.

    I switched to a custom two-bone IK solution, and I suggest doing the same. Cheers.
     
    YondernautsGames likes this.