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. Dismiss Notice

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

Discussion in 'Animation' started by menshoy, Dec 17, 2022.

  1. menshoy

    menshoy

    Joined:
    Nov 17, 2022
    Posts:
    1
    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:

    Code (CSharp):
    1.  
    2.     void Update()
    3.         {
    4.             if (rb.velocity.x == 0) {
    5.                 SetState(AnimationState.Idle);
    6.             } else {
    7.                 SetState(AnimationState.Walk);
    8.             }
    9.  
    10.             if (Input.getKeyDown(KeyCode.Space)) {
    11.                 animator.SetTrigger("Attack");
    12.             }
    13.         }
    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.
     
  2. unity_I8FaTX-tZFc4lA

    unity_I8FaTX-tZFc4lA

    Joined:
    Aug 23, 2020
    Posts:
    4
    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. https://docs.unity3d.com/2023.1/Documentation/Manual/class-Transition.html

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