Search Unity

Discussion What is the correct way to calculate Foot IK values for animation clip?

Discussion in 'Animation' started by makaolachsiungca, Sep 4, 2023.

  1. makaolachsiungca

    makaolachsiungca

    Joined:
    Sep 27, 2019
    Posts:
    31
    Unity 2022.3.3f1

    Work Through:
    1. Extract global position and rotation from animation stream by animationScriptable job.
    2. Converting IK goal coordinate into posture space(character mass center).
      2.5.Adjust foot IK goal rotation for unknown reason by using rotation offset (0,90,-90).
    3. Apply clip curve with correct editor binding.
    However this work through will mess actual IK position and rotation with humanoid scale issue.
    Is there a way to process these IK converting?

    Code (CSharp):
    1.  public void ProcessAnimation(AnimationStream stream)
    2.             {
    3.                 isHuman = stream.isHumanStream;
    4.  
    5.                 var humanStream = stream.AsHuman();
    6.  
    7.                 leftFootGoalFromPosePosition = humanStream.GetGoalPositionFromPose(AvatarIKGoal.LeftFoot);// *humanStream.humanScale;
    8.                 leftFootGoalFromPoseRotation = humanStream.GetGoalRotationFromPose(AvatarIKGoal.LeftFoot);
    9.  
    10.                 rightFootGoalFromPosePosition = humanStream.GetGoalPositionFromPose(AvatarIKGoal.RightFoot);// * humanStream.humanScale;
    11.                 rightFootGoalFromPoseRotation = humanStream.GetGoalRotationFromPose(AvatarIKGoal.RightFoot);
    12.              
    13.                 leftHandGoalFromPosePosition = humanStream.GetGoalPositionFromPose(AvatarIKGoal.LeftHand);// * humanStream.humanScale;
    14.                 leftHandGoalFromPoseRotation = humanStream.GetGoalRotationFromPose(AvatarIKGoal.LeftHand);
    15.                
    16.  
    17.                 bodyPosition = humanStream.bodyPosition;
    18.                 bodyRotation = humanStream.bodyRotation;
    19.             }

    Code (CSharp):
    1.         /// <summary>
    2.         /// Compute animation clip foot IK.
    3.         /// </summary>
    4.         /// <param name="bodyRotation">Character mass center world position.</param>
    5.         /// <param name="fromPoseRotation">Avatar IK Goal world position.</param>
    6.         /// <returns>Loacl rotation for animation clip(in avatar space).</returns>
    7.         public static Quaternion GetFootGoalRotation(Quaternion bodyRotation,Quaternion fromPoseRotation)
    8.         {
    9.             return (Quaternion.Inverse(bodyRotation) * fromPoseRotation)
    10.                 // offset this rotation to adjust foot IK space for unknow reason.
    11.                 * Quaternion.Euler(0, 90, -90);
    12.         }
    [Calculate Foot IK Result]
    upload_2023-9-4_19-46-9.png

    [Without Foot IK]
    upload_2023-9-4_19-40-2.png

    [Using Foot IK]
    upload_2023-9-4_19-40-46.png