Issue: generic animation state switcher with Any State and trigger animations

Hi folks!

I faced with issue when handling a lot of animations of character. I have method in my class which takes the current physics (velocity vectors, collisions etc) and changes animation state.
At first I made transitions between states, but then I realized that almost any state can transit to any other, except some of them, so I started using Any State. And now I have an issue with trigger animations. When something triggers animation, Animator switches to given animation but then according to update animation method it switches between trigger animation and some other one and trigger animation actually never played.

The easies way to reproduce it is create 3 animations and make transitions from Any State, and update them like this:

    void Update()
        {
            if (rb.velocity.x == 0) {
                SetState(AnimationState.Idle);
            } else {
                SetState(AnimationState.Walk);
            }

            if (Input.getKeyDown(KeyCode.Space)) {
                animator.SetTrigger("Attack");
            }
        }

I can’t find some good solution. Some of things came to mind:

  • block animation state changes while character is attacking, which sounds really inconvenient
  • give up with the Any State approach, which is not cool because if you have N states each of one can transit to each other it will N^2 - N transitions which is impossible to handle if N grows up to 5+
  • give up with generic animation switcher and set parameters not every frame but only if it required. This approach is really hard to implement considering that I wanna use same physics logic for controllable and NPCs

But every approach sounds not really cool, I’d like to keep single method responsible for calculating current animation state and usage of Any State. Maybe there’s animation parameters order, to help Unity understand which transition to choose? I’m not sure as I’m not advanced in Unity.

I might be wrong, but I think the usual approche to Idle/Walk is to use setFloat on the Animator. Also, I’m not sure what the function SetState is suppose to do.

You might want to look into the Exit Time of a transition. Unity - Manual: Animation transitions

An advice for you would be to consider doing a complete Tutorial before launching yourself in the best that is Unity.