Making a Hack’n’slash #3: Importing a 3D model & animating our hero 1/2

Let’s keep working on our hack’n’slash and import a Mixamo character…

This article is also available on Medium.

So far, we’ve set up a few nice mechanics to have our player move around (both with the keyboard or a gamepad controller), and also make the camera follow it all throughout the scene.

But at the moment, our player is just a red capsule. This kind of simple shape is cool and all, because it’s directly available in Unity as a primitive; but, to be honest, this doesn’t cut it for lots of games! Even in low-poly and arcade games, chances are that you’ll have some custom meshes and 3D models, made in a 3D software (like Maya or Blender for example), that you’ll want to import in Unity.

Sometimes, you’ll even have animations in your 3D model.

In this couple of episodes, we’re going to discuss how to import an asset, configure its settings and set up its Animator to play various animations corresponding to our Idle and Run states…

Today, we’ll begin by downloading a model and adding to our project with the proper settings – so, in the end, we’ll have a simple guard model for our hero:

Downloading and importing a 3D model

As you might know, I’m more of a programmer than a 3D artist. Even if I dabble in the world of CGI, I’m clearly not a professional modeler, and I’m really not great at animating. These are actual jobs that require you to study and practice, and these are not my craft 😉

But! Nowadays, thanks to the numerous libs, packages and bundles that are available online, it’s totally possible to find medium to high quality stuff for your prototype projects… and a large portion of those assets are even able for free!

So, in this series, I’ll be using a free character model (and associated animations) that I downloaded from Adobe Mixamo, which is an online platform where you can download rigged characters, poses and animations for free! So even if you don’t have your own 3D model, you can easily download any animation with one of the built-in avatars… and you’ll see as you browse the catalogue that there are quite a lot of animations available. The lib has a particular focus on video games projects so you’ll easily find animations of a biped standing, walking, running, jumping or attacking for example.

Note that before you can download a character or an anim, you’ll need to log in to Mixamo – you can either create an account or use a common sign-in provider like Google.

Once you’ve logged in, you can go to the “Characters” tab and go through the list to pick one that you like. You can choose whichever you prefer because the really valuable thing with Mixamo characters is that they are all rigged the same, and so they will all be able to use the same animations 🙂

I personally went with a little guard model, the “Castle Guard”:

You can get it directly in the Github of the tutorial, too! 🚀

If you want, you can download the character model directly, simply by clicking on the “Download” button in the column on the right.

But, usually, it’s easier to just “pack” this model with some animations – so let’s now get some cool animations! Again, it’s up to you to find the ones that you like the most. In my case, I’ve browsed the lib and took a few animations to anticipate for the following states: idle, running, punching (with 2 variations) and taking a punch (with several variations).

Note: I don’t plan on having my character jump in this game, but if you do you should also look for a jump animation, and perhaps something for the falling state, too…

Of course, I might need to come back to this lib later and download additional animations (such as something for when my character dies, for example), but it should be enough for now 🙂

To download an animation (with the character model “embedded” in it), all I have to do is click on the “Download” button in the right column – this opens a panel with some export settings. Make sure that, for at least one animation, you do export the model “with the skin” (that’s how you can get the character model inside your exported file), and leave the rest to the defaults:

Of course, once your character is embedded in one animation, you don’t necessarily have to add it to the others – as we’ll see very soon, we will be “sharing” this model between all animations 😉

Once you’re done browsing and downloading, you’ll have several FBX files to import into Unity: just drag and drop them in your Assets/ folder or any subfolder to have Unity automatically import them into your project!

Configuring the import settings

The problem is that, for now, our FBX files are not properly configured to be used in Unity for our hero. All the data is here, but we need to make sure it is properly extracted by the engine! There are a few import settings we need to change…

First, select the (or one of the) FBX files with your model embedded (i.e. one that you exported with the “with skin” option turned on). You’ll see that the Inspector now shows you the FBX importer window, that has four tabs: “Model”, “Rig”, “Animation” and “Materials”.

Let’s go through all of these tabs in order!

Model

The “Model” tab is mostly a way to change the axes or scale of your model, select just a subpart of the file (for example by excluding the lights or cameras in a more complex FBX file) or overwrite some settings on your mesh faces, vertices and normals data.

All in all, Mixamo models are designed with game engines like Unity in mind, so the defaults are usually okay. We can however make sure to “bake the axis conversion” so that we don’t have strange rotations on our model.

Also, when you’re done applying any import setting, don’t forget to hit the “Apply” button at the bottom of the window!

Rig

We’ll have a bit more work to do in the “Rig” tab. This is where we are going to generate and associate an avatar to our 3D model.

In Unity, if you want to animate a humanoid character, you’ll need to define an avatar for it. As explained in the docs, this asset is here to “reconcile the bone structure of the Model to its Animation”, it basically allows you to do an (automatic) re-mapping of the bones stored in your FBX file with in a Unity humanoid avatar, so that the FBX skeleton and all its animations can be played by Unity’s animation-related components.

To create an avatar based on our FBX, all we need to do is select the “Create From This Model” option (and again, apply the settings):

You can also toggle the “Optimize Game Objects” checkbox to have Unity automatically “strip” all the inner bone game objects from your Unity armature, so that it requires less computation during runtime. If you want to re-expose the hands, for example, to anchor some weapons on there, all you have to do is use the “Extra Transforms to Expose” option below and select the specific bones to show game object for! 😉

If you take a look at the FBX asset you selected and you’re currently editing, you’ll see that it now contains a new sub-asset, the auto-generated avatar:

Animation

Next on our list is the “Animation” tab. This is where you can check the different animation clips that are stored inside your FBX file: in our case, each FBX file only contains one clip named “mixamo.com”.

So the first thing we can do is rename this clip to give it a more meaningful name. Then, for the “idle” and “running” animations, I need to insure that they loop – whenever the clip finishes, it should restart from the beginning. Note that this is not true of all clips: single actions like a punch or a jump should only play once!

Again, those clips are stored as sub-assets in our FBX assets.

Materials

Finally, the “Materials” tab allows you to choose how we want the materials to be assigned on our model. In particular, we can choose if the materials are extracted from the FBX file, overridden by Unity assets, etc.

In my case, I have some materials inside my FBX file that I want to retrieve. This can be done very easily by extracting both the textures and materials from the FBX asset into actual Unity assets – in other words, rather than having them as sub-assets, we want to generate real Unity materials and images in our project.

When you click on the “Extract Textures…” button, Unity will open a file browser and you’ll be able to pick a folder where to extract all the textures stored inside the FBX:

In this specific case, the model contains three textures that all contribute to the material to give it its colour, its shading, its cavity effects, etc. In particular, there is a normal map that “simulates” a relief on the surface – I won’t go into all the details but if you want to learn more about normal mapping, you can check out the wiki 😉

But anyway – I need to make sure that this image is considered as a normal map by Unity by setting the right Texture Type:

Now that my textures are ready, I can click the other button in my “Materials” tab and also “extract the materials” – Unity will automatically relink the textures we extracted before and re-inject them inside the newly created material:

Wrapping up the configs

Ok – we’ve finished preparing our first FBX file! We now have a basic 3D model all rigged and textured, ready to be animated in Unity that we can drag in our scene:

You’ll notice that it has a two-levels hierarchy because the mesh is “nested” inside a top root object.

However, we will see in the last part of this article that we need a bit more control on our object and its rotation. So let’s add another level with an empty game object that encompasses the two other objects on which we re-add our CharacterController, CapsuleCollider and PlayerController components. This way, we’ll have the same set up as before, on our basic red capsule player (just be careful that the controller and collider dimensions and centres match the 3D model extents):

While you’re at it, remember to re-assign the tracking target of the Cinemachine virtual camera, too:

For the other FBX files, we need to do the following:

  • in the “Model” tab, it’s the same as before: just check the “Bake Axis Conversion” option
  • in the “Rig” tab, we don’t need to re-create an avatar – instead, we can just relink this FBX (and more importantly its animations) to the same avatar asset by picking the “Copy From Other Avatar” option:
  • in the “Animation” tab, you can rename the clip and toggle the “Loop Time” checkbox if need be
  • in the “Materials” tab, since we already prepared our model and its materials, we can just ignore everything by picking the “None” material creation mode:

And we’re done with the import settings! We can now use the 3D model and the various animations we got from Mixamo for our hero…

Conclusion

In this episode, we saw how to import a 3D model from Mixamo and we now have nicer visuals for our hero than our initial red capsule:

Next time, we’ll see how to set up an Animator and link it to our previous move logic so that our hero can transition between an idle and a run animation when we activate or deactivate the move input…

Leave a Reply

Your email address will not be published.