Search Unity

How do I group a set of animations in an animator to reduce transition duplication?

Discussion in 'Animation' started by FrickinSilly, Jun 28, 2020.

  1. FrickinSilly

    FrickinSilly

    Joined:
    Feb 19, 2020
    Posts:
    53
    Say I have 10 animations for a character: Idle, Walk, Run, Jump, Knocked Down and 5 different attack animations Attack1, Attack2, etc..

    I want to trigger each of the attack animations using an Integer parameter. e.g.
    Code (CSharp):
    1. anim.SetInteger("Attack", 1)
    for Attack1, 2 for Attack2, etc.

    If the character is in Idle, Walk, or Run, the player can attack. If the player is in Jump or Knocked Down, the player cannot attack. Therefore, I can't simply transition from AnyState to each attack.

    Obviously I can naively create every single transition in the graph (i.e. Idle to Attack1,2,3,... and Walk to Attack1,2,3.. etc. and then a transition back to each of Idle, Walk and Run), but that seems like I am wastefully duplicating effort. That would be 3 * 5 = 15 transitions to attack and 15 backwards from attack animations.

    Is there no tool for making a transition like: "Attack > 0, go to the AttackSet state machine" and then in that state machine make a finer grained decision as to which attack animation to play based on the Attack Int?

    Blend trees don't seem appropriate here. They are for creating smooth transitions between two animations given a float.

    I played around with Sub-State machines, but I couldn't get the desired effect. If this is the right tool, does anyone have a tutorial for my use case? Most tutorials involve creating a Random animation.
     
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    One of the biggest problems with Animator Controllers is that as soon as you need to do something slightly beyond the basic features they support then you need to hack around and end up wasting time to create an even more convoluted mess than usual.

    anim.CrossFade("StateName", fadeDuration) would let you start a fade to any state without needing to set up a transition, but then you are hard coding the duration without the ability to preview what it will look like and it's entirely up to you to check which state it is currently in to determine if you actually want to do that. Back when I still used Animator Controllers, that's how I did everything but it's far from ideal.

    That's why I made Animancer (link in my signature) which lets you avoid Animator Controllers entirely and just control everything with scripts. That lets you create any logic you want using the full power of C# instead of arbitrarily scattering parts of your logic between scripts and the limited visual interface in Animator Controllers.
     
  3. FrickinSilly

    FrickinSilly

    Joined:
    Feb 19, 2020
    Posts:
    53
    Neat! I'll check through your Animancer library and see if it makes sense for my actual use case.

    Thanks!