Search Unity

How should I set up my Animator for this?

Discussion in 'Animation' started by Marscaleb, Jan 24, 2014.

  1. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    1,037
    My animation tree (or web) is getting a little big for my main character. Well I guess I knew that was going to happen.
    Anyway I'm looking for some advice for how I should be setting up some animations so that they work properly and understandably. I'm working with sprites and 2D, but I don't think this really changes the basic pattern of how I should go about this.

    $anim.png

    Right now I want to add an animation for getting hurt while crouching, an furthermore, I have a few more getting-hurt animations that I am going to be adding later. Hit that makes the player airborn, hit that throws the player on his back, and a number of hit by special attacks with unique animations.

    I've learned that if you use a bool as a transition from "any State" you will have that animation constantly restarted every frame the bool is active. So my hurt and crouch states currently use triggers to make a transition from "any state." This confounds my ability to play something for when I am both crouching and getting hurt.

    While I suppose I could create a bool for crouch as well as the trigger for crouch, this method is going to create a large array of hurt functions coming off of "any State" and make a complicated mess of making sure that each one gets played right. I'd have transitions from "any state" that include hurt and this bool hurt and that bool, hurt and this trigger, hurt and that trigger, and then a list of custom triggers for getting hurt from certain types of damage.

    I also thought about having the transition to couch-hurt only come from crouch (and the other crouching animations I haven't made yet) and then have the regular hurt animation be transitioned from all the other states. But this is a lot of states to be transitioned from, especially when I add in more animations for actions I don't have yet. Just for right now I'd have to make a transition coming from seven nodes, and then once I add the other hurt animations they would be coming from this monsterous list of basically every state except my crouching animations.

    If I just have all of my different hurt animations branch off of my regular hurt animation, ie simply have my base hurt animation get triggered from any state and then all my other hurt animations transition from that one, then they will all play the first frame of that animation before they play their own (if I understand this correctly.)

    I looked into creating a blend tree, but that only responds to a single float value; I can't use it as a staging area to link to the appropriate animation node by checking a myriad of different qualities. I suppose I could create a float that I treat like an enum, and each value corresponds arbitrarily to a different animation, but then I's still have a complicate array of actions to follow out of that tree, since each animation returns to a different state.

    Nothing here sounds like a smart way to go about this.
     
  2. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    1,037
    No one? I'm just looking for some ideas...
     
  3. Kale

    Kale

    Joined:
    Aug 25, 2011
    Posts:
    103
    Why not just create a different layer with the hurt animations, and have it override your current animation with a mask that'll limit the body parts affected. If you do that, you can probably just get away with 1 hurt animation. Then make the weight like .5 so that you'll get a mix of both couch or idle with hurting.
     
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    If you want all the hurt animations for variety or realism, you can still put them all on a higher layer. When Hurt is false (the default), stay on a null state that has no animation clip assigned. When Hurt is true, transition from the null state to the appropriate hurt state. If you don't use a mask and you set the layer weight to 1, the hurt animation will completely override the base layer's animation. And when it's done, transition back to the null state. No need to use "Any State" at all.

    You can blend on two floats using 2D blend trees. But, perhaps more relevant to your scenario, you can have nested blend trees. You can use Float parameters as "Booleans" by setting them to 0 or 1.

    For example, you could have a Locomotion blend tree that blends on a Crouch float, with sub-trees Upright and Crouching. When Crouch==0, the Upright sub-tree has full weight. When Crouch==1, the Crouching sub-tree has full weight. (You could do a half-crouch by setting Crouch to 0.5.)

    Then the Upright sub-tree can blend between Idle and Run based on Speed. Likewise for the Crouching sub-tree.

    This may sound more complicated than your current state machine, but once you have it set up it's actually pretty simple. It's extensible, too. In one project, I replaced the blend tree's Idle clip with a 2D blend tree based on Gender and Swagger. This tree blended between four clips to give personality to each character's walk cycle:
    1. Male plain walk
    2. Male cocky walk
    3. Female plain walk
    4. Female hip-swishing walk

    Another entirely-different option in Unity 4.3 is to manually transition to the right hurt state in code using Animator.Play() or Animator.CrossFade().
     
  5. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    1,037
    I can just f***ing tell it what animation to play in the code?

    Dear word, this changes everything!

    Also, the animation layers are geared toward animating different parts of a body. I'm just using whole animations.
     
    Last edited: Jan 27, 2014
  6. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    Yup, they finally introduced Animator.Play() and Animator.CrossFade() in 4.3.
    Gotcha. I use an additional layer for whole-body additive animations, on top of the whole-body base layer, but that's because it's additive. But there's no reason why you couldn't use another whole-body layer to completely override the base layer. In fact, at one point I was playing with having each weapon style (pistol, rifle, sword, etc.) on its own whole-body layer, and just setting the weight of the active weapon to 1 -- in essence, sync layers in Unity Free. It seemed to work pretty well.