Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved OnAnimatorIk Never called

Discussion in 'Editor & General Support' started by Lyam6666, May 19, 2020.

  1. Lyam6666

    Lyam6666

    Joined:
    Nov 8, 2017
    Posts:
    2
    Title self explanatory. Just a basic project, I've got some animations for a 3rd person character with Character Controller and i'm trying to rotate the upperbody according to mouse. I've researched a bit and found that I needed:

    - Already setup animations in my Animator (wich I already had since the beginning)
    - Ik pass is checked in base layer of the Animator (wich was already checked too)

    Here is my code:
    Code (CSharp):
    1. public class MouseLook : MonoBehaviour
    2. {
    3.  
    4.     public Transform playerTransform;
    5.     public Animator animator;
    6.     private Transform chestTransform;
    7.  
    8.     public PlayerShootingScript shootingScript;
    9.    
    10.  
    11.     public float sensitivity = 100f;
    12.  
    13.     private Vector2 mouseInputs;
    14.     private Vector2 MouseInputs
    15.     {
    16.         get
    17.         {
    18.             return mouseInputs;
    19.         }
    20.     }
    21.  
    22.     private float xRotation = 0f;
    23.  
    24.     // Start is called before the first frame update
    25.     void Start()
    26.     {
    27.         Cursor.lockState = CursorLockMode.Locked;
    28.         chestTransform = animator.GetBoneTransform(HumanBodyBones.Chest);
    29.     }
    30.  
    31.     // Update is called once per frame
    32.     void Update()
    33.     {
    34.         mouseInputs.x = Input.GetAxis("Mouse X") * sensitivity * Time.deltaTime;
    35.         mouseInputs.y = Input.GetAxis("Mouse Y") * sensitivity * Time.deltaTime;
    36.  
    37.         xRotation -= mouseInputs.y;
    38.         xRotation = Mathf.Clamp(xRotation, -90f, 90f);
    39.  
    40.  
    41.         transform.localRotation = UnityEngine.Quaternion.Euler(xRotation, 0f, 0f);
    42.      
    43.         playerTransform.Rotate(playerTransform.up * mouseInputs.x);
    44.     }
    45.  
    46.     void OnAnimatorIK(int layerIndex)
    47.     {
    48.         Debug.Log("This is never called");
    49.         animator.SetBoneLocalRotation(HumanBodyBones.Chest, UnityEngine.Quaternion.Euler(0f, 0f, xRotation));
    50.         animator.SetLookAtPosition(shootingScript.targetedPos);
    51.     }
    52. }
     
  2. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    The script that like to revive the OnAnimatorIK callback has to be on the same gameobject as the animator causing the event.
    If your script can not be on that gamobject you can have a small script that catches the event and calls the function in your main script.
    Sadly the documentation does not do a good job of explaining it.
     
  3. Lyam6666

    Lyam6666

    Joined:
    Nov 8, 2017
    Posts:
    2
    Yep, I've seen it this morning, forgot to update the post
    Thanks anyway, I'll know it from now
     
  4. arslanmughal

    arslanmughal

    Joined:
    Dec 22, 2015
    Posts:
    4
    Thanks fffMalzbier. That really worked.
     
  5. Recluse

    Recluse

    Joined:
    May 16, 2010
    Posts:
    485
    All OnAnimatorIK scripts that worked prior to updating to 2021.2.14f1 no longer work. Script is on same gameobject as Animator, IK pass is enabled in base layer. Is there something that changed in 2021, that hasn't been documented? I am seeing this on every project in Unity 2021. What has broken?
     
    pachermann likes this.
  6. Default_DD

    Default_DD

    Joined:
    Jul 3, 2022
    Posts:
    4

    So here are some important facts that we need to be aware of:

    1- The script that contains the actual logic for IK needs to be attached to the GameObject that has the Animator component directly.

    2- The new "Animation Rigging" package conflicts with the built in IK (for some reason that I don't know :/). So, if you want to use IK, then make sure the package is removed.

    Hope this helps. I got this information from different sources.
     
  7. Kokowolo

    Kokowolo

    Joined:
    Mar 26, 2020
    Posts:
    52
    Had same issue, but the responses above did not help me. I found this response here by Djohxnn that worked!

    "Avatar animation type needs to be humanoid... doesn't work with generic"
     
  8. motif_nostalgia

    motif_nostalgia

    Joined:
    Apr 8, 2019
    Posts:
    2
    hello y'all, if anyone is still stuck like i was reading this here's what fixed it:
    -rig has to be humanoid
    -in the animator, whatever layers you are using have to have "IK Pass" enabled in the settings
    upload_2022-12-2_21-28-32.png
     
    fffMalzbier and TreyH like this.
  9. CharlesJung

    CharlesJung

    Joined:
    Sep 14, 2012
    Posts:
    1
    Thanks to you, I solved it.
     
    TreyH likes this.