Wednesday, April 29, 2015

c# - Unable to use Maya animation with scripts when imported to Unity


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.



Answer



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();
}

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.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...