Today, let’s continue our RTS: we’ll see how to import some 3D models and animate them!
This article is also available on Medium.
Up til now, we’ve mostly been using placeholders or primitive shapes for our various RTS units. Even though we’ve worked a bit on our buildings, the characters (like the Soldier and the Worker) are still cubes or spheres.
But when making a video game, you usually want your characters to be more advanced models with nice textures and animations!
So in this tutorial and the next one, let’s look at importing 3D models into Unity to get more complex meshes than primitives, how to setup an animation system and how to control it using our C# scripts. In this first part, I’ll focus on the imports and basic preparation.
Important note: throughout this tutorial, I will be using a low-poly character model I made recently: a little knight. It’s available on my CG Trader account if you want to get it – but you can also get any other asset that you’d like and follow the same workflow 😉
How do you share a 3D model?
Exchanging visuals or assets
Nowadays, sharing your 3D creations has become pretty universal and it’s easy to distribute your screenshots, animation clips or even 3D models on the net. Websites like Artstation are well-known hubs for CG artists where you can exchange pictures or movies and join a community of creators. On the other hand, Turbosquid or CG Trader are platforms where you can buy and sell 3D assets.
All of this makes for vibrant and creative communities that collaborate on large libraries and allow virtually anyone to find anything they need!
3D models and formats
When you download a 3D model from an online store or any kind of exchange platform, you’ll quite often have a bunch of files with various formats.
Some formats are called “native“: they are file formats that are specific to a given piece of 3D software, like Maya, Cinema 4D, Blender… Basically, they describe a complete scene that you can re-open in these tools to get back the whole project.
Then, you also have some lighter export formats that contain all or part of the info on your model but not all the scene settings around it. Pretty common formats are .obj or .fbx for example.
All these formats have advantages and drawbacks. Some will contain only a subset of the data on your model, and so you might need to do several exports to properly represent it (e.g.: the Alembic .abc format can only contain one animation, so I had to export one file per animation for my CG Trader low-poly character product). Others just don’t understand a specific type of data (for example, a .obj can’t contain an actual animation – you would need to export each frame as its own .obj!).
Finding the right share format can be tricky, but usually the more formats you can export, the better it is because you increase the chances that the end-user will find one that matches their workflow 😉
Importing a FBX model in Unity
Here, I’m going to describe a very common case: how to import a 3D model stored as .fbx into Unity. The FBX format is quite often used for 3D models because it can contain materials, textures and animations – so it’s nice for centralising everything easily!
Let’s take the example of my low-poly knight model. The archive contains 3 FBX files: the knight himself, his sword and his shield.
The first file (
knight.fbx) has a rigged armature (a skeleton) and some animations because I want the knight to actually move! The other two (
knight-sword.fbx) are just basic objects without any animation data – those are accessories that I’ll attach to my knight’s skeleton in the right place to automatically have them follow the motion 🙂
To import all of those files in my Unity projects, I just need to copy them into my project assets folder:
If I select one of them, I see that Unity has a specific Inspector panel for importing FBX assets, with 4 tabs:
- “Model”: to configure the base options of the model (like the axis and units conversion to use), the types of objects to import from the file and other settings on the geometry and UVs of the meshes
- “Rig”: to import or create an avatar for the model in Unity that can then use the animations in the model, if there are some
- “Animation”: to get back the various animations in the model and set their options (e.g.: whether or not they should loop…)
- “Materials”: to configure the materials and textures on your model – those can come from different sources or even be re-created and re-assigned in Unity!
Note that, at this point, you can already drag those .fbx assets inside a brand new scene and you’ll get a little knight with his shield and sword… except he’s all white and not moving!
So let’s see how to set him up to look a bit better 😉
Setting up our character import settings
The settings in this tab depend on your model and how it was made/exported. The options you’ll most likely need to check and look up are:
- the scale, units and axes settings: sometimes, the different 3D tools use different units and scales. For example, a Blender file may be exported with a different unit system than Unity, resulting in very large models. If that happens, you might need to check the scale factor or disable the units conversion.
Similarly, the axes can differ. For example, again in Blender, the up vector is Z while the forward vector is Y, whereas in Unity the up vector is Y and the forward vector is Z. If the model was exported with the Blender axes, then you may need to enable the “Bake Axis Conversion” toggle and then play around with the rotations to get the expected orientation!
- the object types to import: most of the times, the assets are exported with just a subset of the original scene and the creator made sure to isolate the relevant objects. But perhaps you don’t even want all of these objects? The different toggles allow you to select just a few object types and dismiss the rest to avoid cluttering your Unity prefabs with unused objects 🙂
Here, my model is exported with a Unity re-import in mind so I don’t need to change any of the default options.
In Unity, the animation of a humanoid character relies on the definition of an avatar: this asset is used to match the bones in your character’s skeleton with the animation keys and curves. As explained in the docs, you need to “reconcile the bone structure of the Model to its Animation“, and so you basically need to do a re-mapping of the bones stored in your .fbx file with in a Unity humanoid avatar.
Here, I’ll just ask Unity to auto-create an avatar for the
knight.fbx file because my model can be re-mapped to a Unity avatar directly. Once I’ve set my “Avatar Definition” option, I need to hit the “Apply” button to actually save these preferences 🙂
For the sword and shield, since they are not animated, I can turn everything off.
You usually don’t have a lot to change in this tab – it’s just about adding or removing the animations you want to re-import, and optionally set some to loop.
For now, I want to import all of my animations on my
knight.fbx and I want to make four of them loop: the Idle, the Idle Attack, the Run and the Walk. I can do this by going down in the Inspector and ticking the “Loop Time” option. Again, I need to hit the “Apply” button to actually save the changes.
You can also use the previewer at the bottom to instantly see what the animation looks like on your model 😉
knight-sword.fbx, just like for the rig tab, I can simply toggle everything off:
This final step depends quite a lot on your model and your personal preferences! Some people prefer to import materials and rely fully on the 3D file; I personally find it easier to re-create materials in Unity, because I think it gives me more control (especially on the shader/textures).
My models use a total of two materials:
knight.fbxhas two materials: the “Base” (for the body/armour) and the “PlayerColor” (for the fur on the helmet and the shoulder pads decorations)
knight-sword.fbxuse the same “Base” material as the knight
I won’t be re-importing them from the FBX file, so I can deactivate this in the importer Inspector on all three FBX files:
Note: again, don’t forget to hit the “Apply” button to keep your modifications! 😉
If you select one of your mesh in the 3D scene, you’ll see that they are now using Unity’s “Default-Material” built-in asset, so the meshes are still all white.
Time to actually import our textures and re-create our materials!
My low-poly knight relies on the colour palette technique to assign colours to large surfaces based on a single image and a clever use of UVs – this is something that I discovered when watching YouTube videos by the amazing Imphenzia, and I’m actually using his gradient palette texture for this model.
The palette is provided in the knight archive, or you can download it here.
Import it in your project (simply copy it to your assets folder somewhere) and then create a new Standard material – finally, assign the texture in the Albedo texture slot:
And at last, drag this material to the 3 meshes, re-assign another material to the “PlayerColor” slot and you’ll get back some colours!
Animating the character!
Preparing an animation system
For now, our character is sadly a bit still: it’s just in its default T-Pose. To be able to change the animation at runtime and make the character pick an animation from the list of available clips, we need to create another asset: an Animator Controller.
We talked about Unity’s animation system briefly a while ago, when we animated our banner sprite, but we didn’t really dived into all of its capabilities at the time…
The Animator Controller is the core component for animating a character in Unity: it allows you to define a set of animation states (each associated to an animation clip) and the transitions between those states, usually with some variables. The component relies on a state machine and it has a really nice visualisation tool that you can access in the “Animator” window (from the Window > Animation > Animator) menu:
For now, our brand new Animator Controller is completely empty, however (simply select your asset to see it in the Animator window):
The first thing I want to do is set my Idle animation as the default state so that, when the game first starts, my knight leaves his T-Pose and becomes Idle. To do this, all I need to do is unfold the
knight.fbx asset to get access at the sub-assets inside it, and then click and drag the Idle animation clip to the Animator window:
Because it’s the only state in my state machine for now, it’s ben assigned as the default state (shown as orange). Also, since we set this animation to loop, the state will automatically repeat the animation indefinitely.
Finally, I can assign this controller to the “Animator” component on my knight object in the Inspector (this component was created automatically when I configured my Rig options and generated an avatar):
If I try this out, I see that when I run my sample scene, my knight goes Idle…
… but the sword and the shield are floating in the air next to him!
Parenting the accessories
Of course, those objects aren’t animated! What we want to do is parent them to the left and right hand of the character so that they automatically follow the movements of the skeleton. That’s fairly straight-forward (but it obviously depends on your model and how its armature is organised):
And here we are – the sword and shield are now properly moving along with the knight during his Idle animation 🙂
Note: to get a more efficient rig, you can use the “Optimize Game Objects” option in the .fbx rig importer Inspector – this will automatically “hide” all the subchildren of your armature root and let you expose just a chosen few, like my left and right hands here, while reducing the required computation.
Today, we’ve seen how to import a 3D model from a FBX file in Unity and how to set it up to properly integrate to our workflow. We’ve also prepared an Animator Controller to be able to change the character’s animation at runtime, and we’ve configured its default state.
Next time, we’ll take a more in-depth look at this Animator Controller and see how to use variables to control the animation from our C# scripts 🙂