Search Unity

How to change "Motion" of an animator state in runtime?

Discussion in 'Animation' started by unitimokccount, Nov 2, 2020.

  1. unitimokccount

    unitimokccount

    Joined:
    Apr 29, 2019
    Posts:
    21
    I'm aware of this link, but I cannot decipher it for the life of me.
    https://docs.unity3d.com/ScriptReference/AnimatorOverrideController.html

    AOC = new AnimatorOverrideController(parentAnimator.runtimeAnimatorController);
    parentAnimator.runtimeAnimatorController = AOC;
    AOC["Fire"] = equippedWeapon.fireAnimation;

    This is all I've managed to come up with, and I'm getting an unusual error that I cannot decipher.
    upload_2020-11-2_21-50-12.png
    I have no idea where to start on even playing this animation if I did figure this part out.

    I'm trying to replace the motion of an animation state called "Fire" with equippedWeapon.fireAnimation.
    Could I get some guidance on how to achieve this?
    Apologizes if this is too code heavy for this forum.
     
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    Animator Override Controllers were designed stupidly (much like the rest of the Animator Controller system).

    You need to override based on the name of the Animation Clip used by the Fire state, not the name of the state itself.
     
  3. unitimokccount

    unitimokccount

    Joined:
    Apr 29, 2019
    Posts:
    21
    That's a very weird way of doing that, and I can agree from trying to work with Animation Controllers in code that it's very weirdly designed. I ended up just using object Override Controllers instead of in code ones
     
  4. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    If you're interested in avoiding Animator Controllers, check out Animancer (link in my signature) which lets you do everything in code. You generally don't need to care about overrides because you can just call animancer.Play(equippedWeapon.fireAnimation) and it will create a new state if that animation hasn't been used yet, but you can also destroy old states or change the clip used by an existing state if you're concerned about memory usage.
     
  5. Deepscorn

    Deepscorn

    Joined:
    Aug 13, 2015
    Posts:
    25
    Can you please give more details on this. Stumbling on same.
    Overriding the clip the AnimatorOverrideController-way, I'll override all the clips, not only the one on state, as I understand.
     
  6. Deepscorn

    Deepscorn

    Joined:
    Aug 13, 2015
    Posts:
    25
    Thanks, want to spend a bit more time to investigate a native way, without third parties