Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Mecanim Basics

Discussion in 'Animation' started by Marquisk2, Nov 25, 2013.

  1. Marquisk2

    Marquisk2

    Joined:
    Nov 25, 2013
    Posts:
    2
    Hi,

    I made a simple Idle, Grab, and Tool animation for a non humanoid character. I did transitions for all three states, and when I play the game all of the animations are played through and looped over and over again. I watched almost half the video, and should have some understanding of it since I'm doing something very basic.

    Do I stop all of the animations and stay in Idle done through scripting?
     
  2. samuelmorais

    samuelmorais

    Joined:
    Aug 3, 2012
    Posts:
    62
    First you have to make sure what are the transition's condition, so that it doesn't change the state unless some parameter changes. Maybe the transitions are configured with "Exit time" condition, which means that it will exit the current state after some time regardless of any other parameter value.
    That way you don't even need scripting to stay on Idle state, but you will need to script a way of getting out of the Idle state, like a key/button pressed changing a parameter of the Animator object, with, for example, animator.SetBool("Grab",true).
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    It may be worth pointing out that since you're scripting anyway, you don't have to use triggers and transitions to get out of your idle state. You can directly cross-fade to a Mecanim animation state, just like you could play a legacy animation.

    In fact I have a little script that supports both, using a method like this:

    Code (csharp):
    1.     void PlayAnimation(string name) {
    2.         if (animator != null) {
    3.             animator.CrossFade(name, 0.3f);
    4.         } else if (legacyAnimation != null) {
    5.             if (curStateName != name) {
    6.                 legacyAnimation.CrossFade(name, 0.3f);
    7.             }
    8.         }
    9.         curStateName = name;
    10.     }
    I'm still new to Unity animation, but so far I'm really liking this approach — it cuts down on the complexity of my animation controllers, while making the code no more complex. (And using the same code with both legacy and mecanim animations is a plus.)