Importing animations from models in Unity

Unity provides a number of tools for animating GameObjects, and, as you know, you can import animations with models. In fact, you’ve already imported some with the space marine and alien! All you need to do is incorporate them into your game, which is pretty easy.

In this tutorial, you’ll incorporate various model animations and connect those animations to an animator component.

Note: This tutorial is part of a collection that teaches Unity development from the ground up. You can read the entire series over here. This series is free and does not require any account creation. All assets are provided. If you find it useful, feel free to buy me a coffee.

Importing models animations

In the Project window, select the BobbleEnemy GameObject from the Models folder. You’ll see four tabs in the Inspector: Model, Rig, Animation and Material

This shows Bobble Enemy import settings

The Materials tab deals with textures that are imported with your model. The Model and Rig tabs deal with the actual model structure.

Avatars and Rigs

Typically, models are rigged, which means Unity creates a system that acts as a virtual skeleton to helps your models move naturally.

For instance, when you move an arm of a model, all the children GameObjects that comprise the arm will move in accordance with the rig’s limitations, as well as any that you dictate.

Rigs in Unity are called avatars and they can share animations. Unity provides a library of free motion capture animations and, with a proper rig, you can incorporate these motions with minimal work.

The Animations tab is where you configure your imported animations. You’re about to get a crash course in all this… starting now!

Importing the alien walking animation

Click the Animation tab. Here are the animations you imported with the model. There are lots of configuration options. You can find your animations in this dialog.

This shows the Bobble Enemy animation settings

Beneath the animation are all tools for tweaking it. The timing bar allows you to edit the length of animation; a common use case is making a seamless loop effect.

Check the box for Loop Time.

This shows the loop time unchecked

Look a little farther down to find the Preview pane. If you don’t see a preview, look for a bar at the bottom and drag it upwards.

This shows how to pull up on the animation viewer

Click the play button and the model will show the selected animation.

This shows the play button on the animation viewer

Once you’re satisfied with the preview, you’ll need to save it. At the bottom of the Inspector, just above the Preview window, click the Apply button.

This shows the apply button on the import animation

Animating the alien

In the Project window, select the Animations folder and click the Create button. From the drop-down, select Animator Controller and call it AlienBody. With the Animation Controller selected in the Project browser, open the Animator window.

Next, right-click in the Animator window and select Create State ▸ Empty. Select the new empty and name it Walking in the Inspector. Next, click the circle beside the Motion property.

You’re looking for Armature.Body/WalkCycle but, in the list of animations, there are many clones. Select the top-most Default Take. Check the footer to see which model owns the animation.

Click through the animations to find the one that belongs to Assets/Models/BobbleEnemy.fbx. Double-click it to select it.

This shows the BobbleEnemy animation selected.

Go to the Prefabs folder in the Project window and select the Alien. In the upper right hand corner, click the Open button.

This shows the Open button circled.

This opens the Alien Prefab for editing. Expand the Alien and select the BobbleEnemy GameObject. Click the Add Component button. Select the Animator component from the Miscellaneous category. From the animations folder, drag the AlienBody animator to the Controller property on the Animator.

This shows the AlienBody added to the controller

Return back to the Scene by clicking the Scenes tab in the Scene view.

This shows the Scenes tab selected.

Now play your game. You should have lots of walking aliens now! The aliens are walking a bit slow. Open the AlienBody animator and select the Walking state. In the Inspector, set the Speed to 2. This doubles the animation speed.

This is the speed set to 2

Play your game again, and now you’ll see some motivated critters!

Importing the space marine’s animations

Now that the aliens mobilized, it’s time to summon the space marine. In the Models folder in the Project view, select the BobbleMarine-Body and click the Animation tab in the Inspector.

You’ll see many unused animations that were imported with the model. This may have been the result of the export to FBX, but in future projects you may have animations that you don’t need.

Delete all of them except Armature|Walk and Armature|Idle by selecting and clicking the minus sign. Unfortunately, you must do this one at a time.

This shows the minus button underneath all the additional animations.

If you mistakenly delete the wrong animation, just click the Revert button at the bottom of the Inspector. Breathe a sigh of relief now that you have a tidy workspace. Select the Armature|Walk animation and check the Loop Time box beneath to loop the animation indefinitely.

This shows the loop time property checked.

Click Apply at the bottom to save changes.

Creating the space marine’s animator

You’ll need to create a transition between the two different states. In the Project window, select the Animations folder and click the Create button. From the drop-down, select Animator Controller. Give it the name SpaceMarine.

In the Hierarchy, expand the SpaceMarine and select BobbleMarine-Body. Drag the SpaceMarine animation controller from the Project browser to the Inspector. This automatically adds an animator and configures it to use the SpaceMarine animator.

With the BobbleMarine-Body still selected in the Hierarchy, switch to the Animator. Create two different states: MarineIdle and MarineWalk. Assign Armature|Idle to the MarineIdle and Armature|Walk animation to the MarineWalk state. Set MarineIdle as the default state.

Now, you need to add some transitions. Right-click MarineIdle and select Make Transition. Drag it to MarineWalk. Do the same from MarineWalk to MarineIdle. Your animator controller should look like the following:

This shows the marine animator states

Click the Parameters tab then click the plus sign. Select Bool from the drop-down and name it: IsMoving.

This shows the plus sign circled on the layers tab in the animator window.

Now, click the transition from MarineIdle to MarineWalk, and in the Inspector, uncheck Has Exit Time. Also, click the plus sign under Conditions, and set the value to true.

This show the configuration settings circled on the animation transitions.

Do the same for the transition from MarineWalk to MarineIdle, but set the Condition to false. When IsMoving is true, the walking animation will play. Otherwise, the marine will stand still.

Writing some additional code

To make your animation work, you need to write a little code. Open the PlayerController script in your code editor and add the following instance variable underneath the others:

[SerializeField]
private Animator bodyAnimator;

In FixedUpdate(), update the code to the following:

if (direction != Vector3.zero)
{
    head.AddForce(((direction) * 150) * -1, ForceMode.Acceleration);
    bodyAnimator.SetBool("IsMoving", true);
}
else
{
    bodyAnimator.SetBool("IsMoving", false);
}

This simply sets the appropriate values on the Animator. Save the code and switch back to Unity.

In the Hierarchy, select the SpaceMarine GameObject and drag the BobbleMarine-Body into the Body Animator property of the SpaceMarine.

This shows BobbleHead-Body being added to the Body Animator property.

Finally, play your game. Now your SpaceMarine can do the moonwalk!

Where to go from here

Bobblehead Wars is really coming along. You have bobbling heads and moving characters. In the process of making all of these animations, you thoroughly explored Unity’s animation engine, Mecanim.

You can download the completed project for this chapter here:

https://drive.google.com/file/d/1oWUtjeiOwMyFrsR1vPgBYM6ZH4MxpKcL/view?usp=drive_link

In the process, you learned:

  • Keyframe animation: The basics of creating an animation clip.
  • Animator: How to use it and manage animations within it.
  • Triggering animations: How to do this in code (like a boss).
  • Outside animations: How to use animations created outside of Unity.

Although the game is coming together, it feels like there’s something missing. How can you have a game without sound?!

You’ll fix that in the next chapter, Chapter 7, “Sound,” where you’ll bring Bobblehead Wars to life with some kickass sound effects and groovy tunes. You do this in the next tutorial.


Discover more from Jezner Blog

Subscribe to get the latest posts sent to your email.

By Brian Moakley

Brian Moakley is a writer and editor who lives amongst the quiet hills in New England. When not reading tales of high adventure, he is often telling such stories to all who will listen.

Related Post

Leave a Reply