4
\$\begingroup\$

I am testing to import Maya animation over to Unity. I set up a simple cylinder with 2 bones and an IK handle. Made a simple animation where the cylinder bends and goes back to straight position over 24 frames.

Following that, I selected everything and baked, all bones,ik,(animation by selecting all at the graph editor) and even the cylinder. I saved the scene and then select all and export as FBX with animation and bake checked.

In unity imported it and at the preview able to see the animation. When I load the model into scene and play (after assigning the controller), able to see animation too.

But now when I try to script it and control the animation, nothing happens. Even to test, I tried the following under the Update method.

if(animation.isPlaying)
    Debug.Log("Animation Works");
else
   Debug.Log("Animation not working"); 

The bool doesn't even return true nor false. My animation is called "bend", thus just for try I did the following and nothing happens.

animation.Play("bend");

Can please advice based on my steps, am I missing something. Do I need to add the controller or is that an unnecessary step? Did I screw up on the Maya part or the Unity part. Thanks for help.

\$\endgroup\$
10
  • \$\begingroup\$ Are you using Mecanim or Legacy animation? If you don't know, then you probably imported as Mecanim because that code is Legacy. \$\endgroup\$
    – jhocking
    Commented Jun 7, 2014 at 1:45
  • \$\begingroup\$ I am not sure which animation I am using. Is there a way to import my animation from maya as Legacy? I didn't see an option to choose between these 2 when exporting from maya. \$\endgroup\$
    – kar
    Commented Jun 7, 2014 at 2:02
  • \$\begingroup\$ I just read up that Legacy is not recommended since it is outdated. docs.unity3d.com/420/Documentation/Manual/Animations40.html. Just making sure I got it right, you mean animation.Play() doesn't work for Mecanim and there is a different API that needs to be used for that? \$\endgroup\$
    – kar
    Commented Jun 7, 2014 at 2:06
  • 1
    \$\begingroup\$ Yes. With Mecanim you tell the animation controller parameters like "speed" and the animation controller decides what animation to play in response. \$\endgroup\$
    – jhocking
    Commented Jun 7, 2014 at 2:15
  • \$\begingroup\$ I am assuming legacy/mecanim are options that happen at the Unity end and nothing to do with Maya. Please correct me if I am wrong. I am looking at the options for my rig. I only see (none, legacy, generic, humanoid). there is no Mecanim option. Please advice where I should be choosing the option Mecanim. It is currently chosen as generic. Thanks for help. \$\endgroup\$
    – kar
    Commented Jun 7, 2014 at 2:23

1 Answer 1

2
\$\begingroup\$

Ok your first problem is that the code there is for Legacy animation, and you probably imported as the default which is Mecanim (actually, in comments we already determined that; just leaving this note here for future visitors). Unity has an older animation system that is still around for legacy reasons, but you shouldn't use that for future projects. However for reference you can switch between animation systems in the Rig tab of the model; there is a drop-down to switch between Humanoid and Generic rigs (both of which fall under the umbrella of Mecanim), or you can set to Legacy.


Anyway, at a high-level the way you use the new animation system is by setting up a bunch of animation states in an Animator controller, assign parameters to transition between those animation states, and then when your code passes values to the animator it will transition between animations. Whereas in Legacy the animations are attached to the model directly and your code plays animations by name, in Mecanim the animations are used in an Animator and the Animator is attached to the model. This indirection is the first thing to understand; it can seem a bit odd since the animation clips can be part of the model (ie. part of the fbx file that was imported) but this indirection also allows you to share animations from other models.

refer to http://docs.unity3d.com/Manual/AnimationParameters.html

Create a new Animator controller, and then open the Animator window to edit it. First click the + button in the lower-left to setup parameters you'll use (eg. a float called Speed). You can drag animation clips into the Animator (click the right arrow on a model asset to see the animation clips) and then right-click the animations to set transitions between them (eg. from Idle animation to Run animation when Speed is greater than 1). Finally select the model in the sene and drag the Animator asset to the animator slot in the Inspector.

At this point the code to trigger animations is pretty simple; Mecanim is designed to handle for you all the complex work of deciding which animation to play and transitioning between them. In your code you need a reference to the Animator component; there are a variety of ways to do that, one of the simplest being GetComponent (this is C# code):

private Animator _animator;

...

void Start() {
  _animator = GetComponent<Animator>();
}

Now passing values to the animator is simply done with commands like:

_animator.SetFloat("Speed", 1.5f);

or

_animator.SetBool("Jumping", true);

This might seem like a lot of rigamarole for just testing an arm bending, and well it is a lot for just that. However Mecanim was designed for the complex animations that happens in games, where you want a character to fluidly transition between a bunch of aniamtions. With animation clips set in the imported model, and animation states set in the Animator controller, there's very little your code needs to do to run the animations.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .