Search Unity

Animation, not Mecanim?

Discussion in 'Animation' started by jaybennett, Dec 7, 2016.

  1. jaybennett

    jaybennett

    Joined:
    Jul 10, 2012
    Posts:
    165
    I have the following requirements for my animation system:

    - Can stand in an arbitrary set of weapons/items held by the character (unarmed, rifle, sword, saw, pickaxe, etc)
    - Each stance has an arbitrary set of possible abilities with idle, moving and death being common to all. for example the pickaxe would have a "mining" action and a "basic attack" action
    - weapons are generated programmatically, i.e. any in-game weapon has around 4 of the possible 30 abilities for that weapon type attached to it

    I am a programmer and find that dealing with the Mecanim editor is really cumbersome and I would rather map my data directly to the animations to play, in code.

    Is there a best practice around doing this, in 2016? I know the legacy system of playing clips was deprecated ages ago.
     
  2. Fabian-Haquin

    Fabian-Haquin

    Joined:
    Dec 3, 2012
    Posts:
    231
    That's exactly why mecanism is there for.

    You will have to make a complete setup with Avatar masks and blend trees.

    Mecanism editor may be simplier than you think, maybe you don't use it correctly?
     
  3. jaybennett

    jaybennett

    Joined:
    Jul 10, 2012
    Posts:
    165
    i still have to manually create the state machine through the UI and map the transitions to parameters manually.

    since i have a state machine in my controller and the action / animation to play in my code for each weapon, i don't want to have to do this manual step.

    is there no animation API that lets me play animations by clip name directly?
     
  4. Fabian-Haquin

    Fabian-Haquin

    Joined:
    Dec 3, 2012
    Posts:
    231
    Well, as a developer I understand your frustration.

    To my knowledge the Animation class is not deprecated but I may be wrong.

    To me, Animation System and Mecanism are two different tools for different usages.

    Animation is very easy to setup for an object that have one animation, Mecanism is not.
    Mecanism is very more useful for complexe character animations setup.

    When Unity made Mecanism, it was to make an easy setup for a character using features like layers and mask to run different animations on different part of the rig.

    Thoses features are not in the Animation API.

    Trust me, if you have to make a complexe setup like your Steve-like character, Mecanism will be many more useful to handle layers and playing more than one animation at the same time on one rig.

    With mecanism you can even handle one bone (like the hand) with a transform while the rest of the animation is still playing using a weight by keeping the IK on the elbow.
    By using animator.SetIKPositionWeight or animator.SetIKPosition
    Exemple: an IA character turning the head to look at you
    (You should be able to do that with Animation API, I did it many years ago but you have to do many more things for that).

    [Edit]
    You can still generate an animator controller by coding:
    https://docs.unity3d.com/ScriptReference/Animations.AnimatorController.html
     
    Last edited: Dec 7, 2016
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    It's not named Mecanism!

    The Animation component is not deprecated. The devs have said (on the forums) that it won't be until the Animator can do all of the things the Animation component can. That would include things like being able to play a clip without an AnimatorController.

    So just use the Animation component as much as you want, really.


    With mechanim, the way to set up what you're talking about would be with sync-layers. You'd attach the currently equipped thing to an invisible bone in your model that you use to position held objects, and then have a sync layer for each possible stance. That would make your animation code not have to care about what weapon you're holding. You just set the correct sync layer and go.
     
    Fabian-Haquin and theANMATOR2b like this.
  6. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    LOL - mecanim. :p
     
    hopeful and Fabian-Haquin like this.
  7. jaybennett

    jaybennett

    Joined:
    Jul 10, 2012
    Posts:
    165
    Thanks for the replies, especially Fabian. Scripting the build of the AnimatorController sounds like a great solution :)

    From a game design POV, I want each weapon in the game to have customizable abilities on it, so even though a weapon in the game might only have 4 abilities, there is a larger set of possible abilites (potentially somewhere between 10-50). Therefore also a much larger set of potential animation transitions. I still need Mecanim to handle a ton of potential transitions.

    Scripting the controller setup might be a lot easier to handle setting up a boat load of transitions!

    But I was also experimenting with "Any State" transitions. I'm basically looking for any animation in the stance (idle, run and all the abilities) to transition instantly to any other state. Has anyone had success with that?

    Right now I'm seeing a lot of lag between changing the mecanim parameter and transitioning the animation as well. Any tips for how to set up the transitions for a snappier response?
     
  8. Fabian-Haquin

    Fabian-Haquin

    Joined:
    Dec 3, 2012
    Posts:
    231
    I'sm verysm sorrysm.

    Yes but I mainly use "triggers conditions" in this case of use.

    Well, multiple things can be responsible for a bad response feeling:

    • Has exit time option in transitions
      • If checked, the animation will finish first before taking this transition
      • If not checked, the transition can be processed at any time
    • Transition timing
      • When and how long should be the transition ?
    • When testing your transitions, keep your animator nodal stuff under your eyes to see what's it's doing
      • I happened to me to have something going wrong like taking a transitions, then going immediatly back, then taking it again because of how I have badly handled it in my C#
     

    Attached Files:

    Last edited: Dec 15, 2016
  9. jaybennett

    jaybennett

    Joined:
    Jul 10, 2012
    Posts:
    165
    Thanks @Fabian-Haquin!

    Getting rid of exit time and lowering the transition timer helped a ton. I have a pretty smooth set of transitions within a stance now. Added 3 different stances with basic attack animations (fistfighting, sword, and rifle) and it works smoothly.

    Most of the "clunky" feel that remains seems to be due to my setup and missing transitions between various animations. It's such a pain to set up every single transition and it will only grow exponentially as I add new stances and abilities (which need to transition with nearly every other ability). I seriously wonder if this is a scalable approach or will the mecanim state machine just fall over at a certain point.

    The only way forward now is to start scripting the creation of the animator controller as you have pointed out is possible. It will take me a bit longer in the short term to figure out but worth it :)
     
  10. jaybennett

    jaybennett

    Joined:
    Jul 10, 2012
    Posts:
    165
    @Fabian-Haquin I've been working on scripting the build of the animator controller, but couldn't see how to set the animation clip of the new state? In the documentation you posted the example doesn't set up any actual clips inside the states, just states and transitions!

    There is an AnimationState.motion field which seems to be what to use but the Unity documentation is basically empty for it:

    https://docs.unity3d.com/560/Documentation/ScriptReference/Animations.AnimatorState-motion.html

    Code (CSharp):
    1. var sm = controller.layers[0].stateMachine;
    2. var animState = sm.AddState("newstate");
    3. animState.motion = new Motion(); // how to properly create this Motion object from an FBX clip?
     
  11. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    A Motion is the superclass for AnimationClip and BlendTree. If you set animState.motion to be an animation clip from an fbx model (using AssetDatabase to get the clip), it'll work.
     
    Fabian-Haquin likes this.
  12. jaybennett

    jaybennett

    Joined:
    Jul 10, 2012
    Posts:
    165
    Thanks @Baste and @Fabian-Haquin ! I completed my AnimatorController builder!!



    Using AssetDatabase loading files by path name seemed hard to maintain so I decided to make a custom Inspector window and a prefab to map my abilities to animations. That way if I move animations around they will keep the link. It turned out really well!

    And for those looking to tweak your transition options to be really snappy, here is what I used:

    Code (CSharp):
    1. controller.AddParameter("Action", AnimatorControllerParameterType.Int);
    2.  
    3. var sm = controller.layers[0].stateMachine;
    4. var position = new Vector3(250f, 0f, 0f);
    5. var yOffset = 50.0f;
    6.  
    7. foreach(var animation in ControllerBuilder.Instance.animationList) {
    8.      var animationState = sm.AddState(animation.Ability.ToString(), position);
    9.      animationState.motion = animation.Clips[0];
    10.      var transition = sm.AddAnyStateTransition(animationState);
    11.      transition.exitTime = 0.5f; // this makes the Inspector preview look nicer
    12.      transition.hasExitTime = false;
    13.      transition.hasFixedDuration = false;
    14.      transition.canTransitionToSelf = false;
    15.      transition.AddCondition(AnimatorConditionMode.Equals, (int)animation.Ability, "Action");
    16.      transition.interruptionSource = TransitionInterruptionSource.Destination;
    17.  
    18.      position.y = position.y + yOffset;
    19. }
    It defaults to 0.1f transition times.
     
    Last edited: Dec 27, 2016
    eses, Fabian-Haquin and hopeful like this.
  13. Fabian-Haquin

    Fabian-Haquin

    Joined:
    Dec 3, 2012
    Posts:
    231
    Really nice!
    When you started this topic I didn't even knew this was possible. I just found the original article of Animator controller scripting while responding to you.

    Thanks for sharing your experience with it!
     
    jaybennett likes this.
  14. jaybennett

    jaybennett

    Joined:
    Jul 10, 2012
    Posts:
    165
    Hmm, only one small problem that I encountered, is when I rebuild the controller then all the links to it on my prefabs get wiped out!

    Any thoughts? I'm thinking I can load the old controller and delete all the states and remake them. Or else somehow load all the prefabs and update them after rebuilding the controller?
     
  15. Fabian-Haquin

    Fabian-Haquin

    Joined:
    Dec 3, 2012
    Posts:
    231
    I guess it create another controller and delete the older one ?
    You can still save it in Resources folder and load it manually using Resources class
     
  16. npatch

    npatch

    Joined:
    Jun 26, 2015
    Posts:
    247
    Prefabs are preconstructed, an animator built in runtime is not. So it changes all the time. Even if you press Apply on the Inspector,while playing in-Editor, it might save current runtime guids and stuff but when you exit, those become invalid,as they get destroyed on Application Quit. At least,that's my understanding of serialization in Unity.
     
  17. Renan-Reis

    Renan-Reis

    Joined:
    Dec 9, 2012
    Posts:
    11