Search Unity

Animator Override Controller not working

Discussion in 'Scripting' started by santiagolopezpereyra, Jun 22, 2018.

  1. santiagolopezpereyra

    santiagolopezpereyra

    Joined:
    Feb 21, 2018
    Posts:
    91
    I have an Animator controller on my player's arms, in which I define the states of the different animations the arms will display. At the same time, each weapon I've created has its own animation for the arms -because the movement of the arms are different according to the weapon the player is hitting with-; animation which is stored in a public variable named armHitAnimation on the Tool script, which defines information of each weapon/tool.

    So I've created my Override Controller, set it as the controller for my Animator component, and I have provided its Controller variable in the inspector with my arms controller. So this Override Controller should be good to go. In my PlayerArms script I reference the Override Controller this way (I learned this way by reading the docs):

    Code (CSharp):
    1.     private Animator animator;
    2.     private AnimatorOverrideController aoc;
    3.    
    4.     void Awake () {
    5.             animator = GetComponent<Animator>();
    6.             aoc = new AnimatorOverrideController (animator.runtimeAnimatorController);
    7.             animator.runtimeAnimatorController = aoc;
    8.         }
    On the same script I have a PlayHitAnimations() function, which is called each time the player hits. I pretend this function to play a Hit animation after doing the following: check if the player is equiped with a weapon; if it is, override the default hit animation with that weapon's animation (stored on that weapon's armHitAnimation variable), and only then play the animation.

    This way, if the player has an axe, the Axe_Hit animation clip I've created for my arms will override the default hit animation. If my player is not equiped with any weapon, the default animation will be played (which is some sort of punch, nothing too fancy). This is the script:

    Code (CSharp):
    1.     public void PlayHitAnimations () {
    2.             if (hand.GetComponentInChildren<Tool>()){ // hand is father game object of all equiped weapons.
    3.                 print("It has a tool!");
    4.                 aoc["ArmHit"] = hand.GetComponentInChildren<Tool>().armHitAnimation; // Override the default ArmHit animation clip with these weapon's armHitAnimation.    
    5.             }
    6.             animator.SetTrigger("Hit"); // Triggers the Hit state, whose animation clip was overwritten if the player had a weapon.
    The problem is: the animation is never overwritten; this is, the default hit animation is played regardless of whether my player has a weapon or a tool equiped or not. What am I doing wrong?
     
  2. WeByte

    WeByte

    Joined:
    Oct 16, 2019
    Posts:
    1
    Hello! if this problem has been solved?