Search Unity

Question How to keep IK Pass enabled when creating an AnimatorOverrideController?

Discussion in 'Animation' started by NexarChile, Jan 22, 2024.

  1. NexarChile

    NexarChile

    Joined:
    Apr 29, 2016
    Posts:
    17
    I have the following problem:

    In my project I have an animated humanoid character, and I have created in the Editor its respective AnimationController with the IK Pass enabled. This way the OnAnimatorIK event is invoked without problems.

    However, since I must load an anim file at runtime, and assign this new animation to that character, I must create an AnimatorOverrideController using as constructor the current runtimeAnimatorController. I am doing it in the following way:

    Create an AnimatorOverrideController:

    Code (CSharp):
    1.    _animatorOverrideController = new AnimatorOverrideController(animator.runtimeAnimatorController);
    2.    _animatorOverrideController.name = "ovContr";
    3.    animator.runtimeAnimatorController = _animatorOverrideController;
    4.  
    5.     _clipOverrides = new AnimationClipOverrides(_animatorOverrideController.overridesCount);
    6.     _animatorOverrideController.GetOverrides(_clipOverrides);
    And then, load the anim file at runtime, and play the animation:

    Code (CSharp):
    1.         _currentClip = UnityEditor.AssetDatabase.LoadAssetAtPath<AnimationClip>("Assets/Animations/newAnimation.anim");
    2.  
    3.         _clipOverrides[_currentClip.name] = _currentClip;
    4.  
    5.         _animatorOverrideController.ApplyOverrides(_clipOverrides);
    However, when I do this, the OnAnimatorIK event is no longer invoked.
    And when trying to read the IKPass by casting the AnimatorOverrideController object created as an AnimatorController, it tells me that the object is null.

    Code (CSharp):
    1. Debug.Log(">> IK Pass: " + (animator.runtimeAnimatorController as UnityEditor.Animations.AnimatorController).layers[0].iKPass);
    NullReferenceException: Object reference not set to an instance of an object

    And it also says:

    Animator does not have an AnimatorController

    How can I keep the IK Pass enabled so that I can invoke the OnAnimatorIK event when creating an AnimatorOverrideController and assign it as runtimeAnimatorController?

    Thank you in advance for your help. :D