Search Unity

Best Practices for Triggering Animations for Abilities?

Discussion in 'Animation' started by realfurbz, Jul 15, 2022.

  1. realfurbz

    realfurbz

    Joined:
    Oct 6, 2021
    Posts:
    7
    I am making an RPG where an input plays a certain animation for whatever ability's key is pressed. What is the best practice/best way to trigger these animations in a fluid manner for reasonably snappy combat?

    Right now I have an idle animation and a movement blend tree that is controlled by velocity and xinput. For ability animations I simply have them in the animator with only a transition back to idle (no transitions to the ability), with Next State set as the interruption source. To play the ability, I use animator.Play("animationName") in my script after detecting key input and it works alright, but I am sure there is a better way to do it.

    I considered using triggers but I have read some not so great things about them, but I am very new to Unity so that could be outdated/wrong information. The only other thing I can think of is to use bools to trigger the animation, then inside Update make a function that checks my animator.GetCurrentAnimatorStateInfo.IsName() and sets the trigger bool to false when the animation is done.
     
  2. Inxentas

    Inxentas

    Joined:
    Jan 15, 2020
    Posts:
    278
    The only thing wrong with triggers is that they are simple. I like to use them for setups like this, for when it doesn't matter in what animation I was (idle, walking) as long as the animation triggers fast. It sounds to me like you are fully willing to sacrifice realism for the sake of gameplay.

    I like to use a trigger for an animation that can happen from any other animation by just adding an arrow from the "any state" block to my animation, using a really short and snappy transition time. Just make sure you don't directly tie the animation to the keypress but to some game logic that makes sense. If you want to use bools that's fine too, the snappyness is in the short transition time between animations, and not the code part of the animator per se.

    You can also consider adding an Event to the end if your animations, so you'll have more exact control over when code runs. I find it a bit messier to work with, but it's nice to be able to tune that as needed.
     
  3. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    The main problem with triggers is that they're dumb and isolated.
    • If you set an "Attack" trigger and the character attacks, that's fine.
    • But if you set an "Attack" trigger and the character can't attack, then it will wait and attack as soon as it can. Free input buffering, sounds great right?
    • Wrong. If you get hit and press "Attack" to set the trigger then also press "Jump" to set that trigger, one of them will happen after you finish getting hit. It won't be the first button you pressed, it will pick one based on the order of transitions in the Animator Controller. And the one that didn't get used will still be set so it will still perform that action potentially several seconds or more after the player pressed the button.
    So in most cases that buffering is actually kind of useless and you need to reset every trigger before you set one, which starts to really defeat the purpose of the whole idea. In that case, you might be better off making a single Int parameter with each value other than 0 causing a transition to a particular state so you can just set it for one frame then revert it to 0. I used that approach in one project ages ago and it worked better than triggers, but was still very tedious to maintain so I wouldn't recommend it.

    The best approach I'd recommend for using Animator Controllers is exactly what you described in your second paragraph because it puts the fewest steps between telling it to play an animation and the animation actually getting played. That means fewer things to set up, fewer things to maintain, and fewer opportunities to make mistakes which introduce bugs.

    That approach works, but the best approach I'd actually recommend is to not use Animator Controllers at all because they make it too hard to develop and maintain clean, reliable logic. I could spend ages explaining everything that's wrong with them, but that would be off-topic. Unfortunately, I can't recommend a specific system because advertising isn't allowed here (except in signatures).