Search Unity

Question OnAnimatorIK Bones rotation

Discussion in 'Scripting' started by oukibt_unity, Sep 19, 2022.

  1. oukibt_unity

    oukibt_unity

    Joined:
    May 6, 2022
    Posts:
    19
    Greetings.
    I have a some problem with animator OnAnimatorIK

    When OnAnimatorIK is calling, i set bones local rotation and after get transform. But transform not considing this rotation and return default values.

    Script visualize bones that got from transform.

    (read comments in script)
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Animations.Rigging;
    5.  
    6. [AddComponentMenu("Scripts/Character/Collider Corrector")]
    7. [RequireComponent(typeof(Animator), typeof(CapsuleCollider))]
    8. public class ColliderCorrector : MonoBehaviour
    9. {
    10.     public bool m_EnabledBoneRotation = false;
    11.     public bool m_ShowGizmos = false;
    12.  
    13.     [Range(0.01f, 0.4f)]
    14.     public float m_FootLength = 0.15f;
    15.     [Range(0.01f, 0.25f)]
    16.     public float m_FootOffset = -0.05f;
    17.     [Range(0.01f, 0.3f)]
    18.     public float m_FootHeight = 0.10f;
    19.  
    20.     [Space]
    21.     [Header("Bones")]
    22.     [Range(0.0f, 120.0f)]
    23.     public float m_LeftUpperLeg = 0.0f;
    24.     [Range(0.0f, 120.0f)]
    25.     public float m_RightUpperLeg = 0.0f;
    26.     [Range(0.0f, 120.0f)]
    27.     public float m_LeftLowerLeg = 0.0f;
    28.     [Range(0.0f, 120.0f)]
    29.     public float m_RightLowerLeg = 0.0f;
    30.  
    31.     private Animator m_Animator;
    32.  
    33.     void Start()
    34.     {
    35.         m_Animator = gameObject.GetComponent<Animator>();
    36.     }
    37.  
    38.     void OnAnimatorIK(int layerIndex)
    39.     {
    40.         if (m_Animator == null) return;
    41.  
    42.         //
    43.  
    44.         // Here i set a local rotation to bones and in PlayMode is working fine
    45.         if (m_EnabledBoneRotation)
    46.         {
    47.             m_Animator.SetBoneLocalRotation(HumanBodyBones.LeftLowerLeg, Quaternion.Euler(new Vector3(m_LeftLowerLeg, 0.0f, 0.0f)));
    48.             m_Animator.SetBoneLocalRotation(HumanBodyBones.RightLowerLeg, Quaternion.Euler(new Vector3(m_RightLowerLeg, 0.0f, 0.0f)));
    49.             m_Animator.SetBoneLocalRotation(HumanBodyBones.LeftUpperLeg, Quaternion.Euler(new Vector3(-m_LeftUpperLeg, 0.0f, 0.0f)));
    50.             m_Animator.SetBoneLocalRotation(HumanBodyBones.RightUpperLeg, Quaternion.Euler(new Vector3(-m_RightUpperLeg, 0.0f, 0.0f)));
    51.         }
    52.  
    53.         // But here i try to get transform bones and it return transform without including bones local rotation
    54.         Transform LeftLowerFoot = m_Animator.GetBoneTransform(HumanBodyBones.LeftFoot);
    55.         Transform RightLowerFoot = m_Animator.GetBoneTransform(HumanBodyBones.RightFoot);
    56.         Transform LeftLowerLeg = m_Animator.GetBoneTransform(HumanBodyBones.LeftLowerLeg);
    57.         Transform RightLowerLeg = m_Animator.GetBoneTransform(HumanBodyBones.RightLowerLeg);
    58.  
    59. // #if UNITY_EDITOR
    60.  
    61.         // Here i debugging this transforms
    62.         if (m_ShowGizmos)
    63.         {
    64.             Debug.DrawLine(LeftLowerFoot.position - (LeftLowerFoot.up * m_FootOffset), LeftLowerFoot.position + LeftLowerFoot.forward * m_FootLength - (LeftLowerFoot.up * m_FootOffset), Color.cyan);
    65.             Debug.DrawLine(LeftLowerFoot.position, LeftLowerFoot.position - (LeftLowerFoot.up * m_FootHeight), Color.cyan);
    66.             Debug.DrawLine(RightLowerFoot.position - (RightLowerFoot.up * m_FootOffset), RightLowerFoot.position + RightLowerFoot.forward * m_FootLength - (RightLowerFoot.up * m_FootOffset), Color.cyan);
    67.             Debug.DrawLine(RightLowerFoot.position, RightLowerFoot.position - (RightLowerFoot.up * m_FootHeight), Color.cyan);
    68.             Debug.DrawLine(LeftLowerLeg.position - LeftLowerLeg.right / 7.5f, LeftLowerLeg.position + LeftLowerLeg.right / 7.5f, Color.cyan);
    69.             Debug.DrawLine(RightLowerLeg.position - RightLowerLeg.right / 7.5f, RightLowerLeg.position + RightLowerLeg.right / 7.5f, Color.cyan);
    70.         }
    71.  
    72. // #endif
    73.     }
    74. }
    and with just animation i get this:
    animation bones detection perfectly


    but when i use SetBoneLocalRotation, i get this:
    bones continue detection as animator without SetBoneLocalRotation, and this is my problem


    Video demonstration:
     
    Last edited: Sep 19, 2022