Search Unity

Different animations for similar states

Discussion in 'Animation' started by Chazney, Nov 28, 2014.

  1. Chazney

    Chazney

    Joined:
    Nov 27, 2014
    Posts:
    15
    I'm trying to figure out the best way to handle different animations for various similar states. The example I have in mind is an idle state for both in combat and out of combat. All transitions from both idle states are the same, there are currently no specific animations for in and out of combat for other things. Below is a simplified state machine showing this, ignoring the fact that both idle states could go in a sub-state machine:



    Both idle and idle_fight serve the same purpose, and are simply switched between based on a Combat boolean. Is there an easier way to group idle and idle_fight such that they can switch between each other based on combat to remove the duplicate transitions between other states? I'm looking for something like the following, where AppropriateIdleState becomes idle when Combat is true, and idle_fight when Combat is not true:

     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    You could transition to an empty hub state that then transitions to idle_fight or idle based on the Combat boolean.

    Or you could change Combat to a float, where 0=non-combat and 1=combat, and use a blend tree that blends between idle and idle_fight. In practice, it won't blend so much as switch from 0 (idle) to 1 (idle_fight).
     
    theANMATOR2b likes this.
  3. Chazney

    Chazney

    Joined:
    Nov 27, 2014
    Posts:
    15
    Hmmm, the first method sounds interesting. I'll give it a shot.

    I thought about the blend tree, and I can solve the blend issues using lerp, but I figured it wasn't a 100% optimal solution because if the number of idle states increases to 3 for whatever reason, blending gets a bit weird to handle (e.g. blending from idle1 to idle3).
     
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    When you increase idle states, the blend tree might be easier than a hub state. Define a float named Stance, where 0=idle, 1=idle_melee, 2=idle_pistol, 3=idle_rifle, 4=idle_bazooka, etc., and blend on it. The blend tree serves as the hub, and the motion fields are the destinations.
     
    theANMATOR2b likes this.
  5. Chazney

    Chazney

    Joined:
    Nov 27, 2014
    Posts:
    15
    Yes, I understand this, but using the example you laid out, I don't see a way to effectively transition between idle and idle_bazooka without blending through all the other states (obviously, the issue also occurs with other blend values that aren't adjacent). As you stated previously, simply setting a stance will switch rather than blend, and of course I would like to avoid this choppy transition if possible just for the sake of aesthetics.

    EDIT:
    Tried out the hub method briefly and just realised it suffers the same choppy transition issue since the hub state contains no animation data and thus transitions don't make sense.
     
    Last edited: Nov 29, 2014
  6. Chazney

    Chazney

    Joined:
    Nov 27, 2014
    Posts:
    15
    Okay, after some messing around, I figured it out.

    I have a nested blend tree, and the first blends two states from 0 to 1 using a parameter called StateTransition. The two nested blend trees are set to the appropriate states (in your example, when switching weapons from pistol to bazooka, the nested tree named StartState is set to 2 and the other named EndState is set to 4). StartState can be set to the current EndState, then EndState is set to the desired state (or just values stored in script if possible). Finally, a lerp can transition StateTransition to smoothly blend between the appropriate states.

    I'm sure the actual state machine will explain it better than I can in words:

    The state parameters can obviously be reused for other animations, such as running while holding a pistol/bazooka, and of course, adding more states is easy as you said it would be with blend trees.

    Thanks for the help and pointing me in the right direction, I learnt a lot.
     
    Last edited: Nov 29, 2014
    theANMATOR2b and TonyLi like this.