Search Unity

Having trouble making base layer animations override upper/lower body animations

Discussion in 'Animation' started by Sendatsu_Yoshimitsu, Nov 24, 2014.

  1. Sendatsu_Yoshimitsu

    Sendatsu_Yoshimitsu

    Joined:
    May 19, 2014
    Posts:
    691
    So I have three layers in my animation tree: Base Layer, which currently has nothing, Upper Body, which contains my equipment and weapon use animations, and Lower Body, which contains my movement animations. Upper and Lower Body both have a weight of 1, and use separate masks.

    What I'm trying to accomplish is to create a third set of animations, in this case martial arts attacks, which are full-body animations and as such need to manipulate the upper and lower bodies simultaneously. To that end, I created an empty state in Base Layer, gave it the animation I wanted, and created a single transition from Any State > TestAnimation with a single trigger condition, TestTrigger.

    To test the animation, on input.getkey(somekey) I execute myAnimator.SetTrigger("TestTrigger");. The animation tree shows my test animation is running, but my character doesn't leave his idle animation in-game. As such, I'm wondering if there's some way I can set the animation in the base layer to override the idle in upper and lower body?
     
  2. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    not really since layer are evaluated in the order that they appear.
     
  3. Sendatsu_Yoshimitsu

    Sendatsu_Yoshimitsu

    Joined:
    May 19, 2014
    Posts:
    691
    Hmm okay, thank you :)
     
  4. medhue

    medhue

    Joined:
    Aug 24, 2014
    Posts:
    176
    Why use a different layer? If your animation is triggered from Any state, then why not just play it on a layer already running? You could mask off the actual animations for all the upper body animations, and then leave the layer without a mask. Then you could trick those strike animations from that upper body layer. It kind of makes sense too, if all your weapons are on that layer also.

    Although I very much like the mecanim system, 1 thing that shocks me, is the lack of a priority system. I've used system that allow you to give each animation a priority. If you tried to trigger an animation, it would always play if it has a higher priority. I've even used system that allow you to prioritize each bone, which is similar to, but better than masking bones.

    Heck, maybe I'm wrong tho, and we can use priorities. Possibly in your own code. I just played around with an AI system which has a priority system.
     
  5. Sendatsu_Yoshimitsu

    Sendatsu_Yoshimitsu

    Joined:
    May 19, 2014
    Posts:
    691
    I think that's exactly what I should be doing, but forgive me if I ask an obvious question to be sure- when you say trick the animations from the upper body layer, what do you mean?

    I'm also a bit confused about how the priorities would work here- I'm picturing using a lower body mask to make the legs work separately at need, then overriding it with the un-layered stuff that is functioning as a de-facto upper body system, but wouldn't the override on the legs always win out over any full-body routines I try to run from the upper body?
     
  6. medhue

    medhue

    Joined:
    Aug 24, 2014
    Posts:
    176
    Sorry, I meant TRIGGER, not trick, lol.
     
  7. Sendatsu_Yoshimitsu

    Sendatsu_Yoshimitsu

    Joined:
    May 19, 2014
    Posts:
    691
    Oh hah, and here I was thinking I'd just learned a new bit of animation parlance. :)

    Triggering from one layer sounds smart, I'll give that a whirl and see if I can't get it working!
     
  8. Sendatsu_Yoshimitsu

    Sendatsu_Yoshimitsu

    Joined:
    May 19, 2014
    Posts:
    691
    So I think that works perfectly- if my rigs start exploding in a million bits I know where to start looking, but otherwise running the combat animations from the upper body works! Thanks muchly for the suggestion :)
     
  9. medhue

    medhue

    Joined:
    Aug 24, 2014
    Posts:
    176
    Hmmm, I just did some quick tests, as I just made a product that overrides just the fingers. It seems, that when you override something, yeah, it is going to override everything else. For my finger overrides, this is not a big deal, as they are only fingers, and you can also trigger different fingers for that action.

    This brings me to another thought tho, Technically, if you made all your layers similarly having animations to match those fully body strikes, then you could make it all work. So, for the leg layer, you would need that same trigger for the full body strike, probably just using the exact same animation in that layer, and then you would have to do the same in the upper body layer.
     
  10. medhue

    medhue

    Joined:
    Aug 24, 2014
    Posts:
    176
    hey, if it works, go with it. lol
     
  11. Sendatsu_Yoshimitsu

    Sendatsu_Yoshimitsu

    Joined:
    May 19, 2014
    Posts:
    691
    Oh that's an interesting idea, having the same animation tree in both layers... you could even cut & paste it, if you set up the tree to run from a single Unarmed point.

    Two related things I'm a bit confused on, if you don't mind: I'd like a lot of the attacks to be able to chamber from any animation, and I don't want to go through the whole rigamarole of systematically adding the same transition to each node. To that end I've taken to adding transitions from Any State to each combat node, then having each of those transition back into idle after the animation runs. Is that what Any State was intended for, or does this have implications I'm not going to notice until I trip over them down the road?

    On a really inane related matter, do you happen to know if mecanim can return the duration of an animation in code? I need to calculate each attack's windup and cooldown, and that's way easier to do if I can simply make it Time.Time + the animation duration, so the combat system won't accept any new inputs until the last animation has cycled out and transitioned back into idle.
     
  12. medhue

    medhue

    Joined:
    Aug 24, 2014
    Posts:
    176
    I think, that is exactly what Any State is for. I don't think you'll have problems, as they are triggered with specific keys, or whatever. So, unless you are randomly triggering keys, you shouldn't have a problem.

    I really have no idea if you can get the time in code, but you should be able to see the exact time in either the animations played from the animation source, in the side window, or you could look at the animation in the animation window, while your character is selected.
     
  13. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    If you need to do it at runtime use the following functions:

    http://docs.unity3d.com/ScriptReference/Animator.GetCurrentAnimatorStateInfo.html

    length return the length of the motion in second
    http://docs.unity3d.com/ScriptReference/AnimatorStateInfo-length.html

    normalizedTime is the current time of the clip, the value goes from 0 to 1, if you want the current time in second do this: normalizedTime * length
    http://docs.unity3d.com/ScriptReference/AnimatorStateInfo-normalizedTime.html
     
  14. Sendatsu_Yoshimitsu

    Sendatsu_Yoshimitsu

    Joined:
    May 19, 2014
    Posts:
    691
    Thank you both very much- this has solved several problems that have been clubbing me over the head for months :)