Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

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

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

  1. thepatchworkhoax

    thepatchworkhoax

    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,466
    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.
     
    jiraphatK, AB498 and thepatchworkhoax like this.
  3. thepatchworkhoax

    thepatchworkhoax

    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,466
    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