Home Game-development Unity 2D Game Development Cookbook

Unity 2D Game Development Cookbook

By Claudio Scolastici
books-svg-icon Book
Subscription
$10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
BUY NOW $10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
Subscription
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
  1. Free Chapter
    Importing 3D Models and Animations
About this book

Unity is a powerful game development engine that provides rich functionalities to create 2D and 3D games.

Unity 2D Game Development Cookbook is a practical guide to creating games with Unity. The book aims to serve the purpose of exploring problematic concepts in Unity for 2D game development, offering over 50 recipes that are easy to understand and to implement, thanks to the step-by-step explanations and the custom assets provided. The practical recipes provided in the book show clearly and concisely how to do things right in Unity. By the end of this book, you'll be near "experts" when dealing with Unity. You will also understand how to resolve issues and be able to comfortably offer solutions for 2D game development.

Publication date:
February 2015
Publisher
Packt
Pages
256
ISBN
9781783553594

 

Chapter 1. Importing 3D Models and Animations

In this chapter, we prepare the assets to build up our game prototype, starting with the process of exporting 3D models from Maya, our 3D editor of choice, into Unity. We also take care of correctly setting up the imported models and animations in Unity Inspector once they get imported.

In this chapter, we will cover the following recipes:

  • Setting up a scene in Maya

  • Using groups to rotate FBX files

  • Exporting FBX files from Maya

  • Configuring imported FBX files in Unity

  • Exporting animations

  • Configuring imported animations in Unity Inspector

 

Introduction


When building up a game, we usually start by importing the graphics assets to actually build up and prototype the gameplay.

In this specific case, we decided to begin with 3D models. Before importing the models, you should take care of bringing in the textures. Feel free to switch between Chapter 1 and Chapter 2, 2D Assets for Unity, which focuses on textures and materials. A cookbook is specifically designed to leave the readers free to access the contents in whichever order they prefer.

We assume that you have the assets to test the operations explained throughout this book; in case you don't, you can download the contents available on Packt Publishing website.

When importing models from a 3D software into Unity, there are several settings to be defined: scales, source materials and textures, rigging and animations, and many others. We will discuss the most important setting soon.

For the importing process to be fully successful, it is also important that the scene in the 3D editor is properly set. When modeling stuff with a 3D editor for a 3D engine, it is important that scales, lights (if available), and cameras match between the scenes, or your models won't fit the game levels properly.

For the recipes of this chapter, we decided to pick Maya as our reference 3D editor. We do not mean that Maya is the best software, but there are plenty of reasons for this choice. Native Maya files are supported by Unity, and the LT version of Maya allows you to perform "one-click-exporting" of Maya scenes directly into Unity (http://videos.autodesk.com/zencoder/content/dam/autodesk/www/products/autodesk-maya-lt/video/send-to-unity-fbx-export-video-1280x720.mp4).

Maya is also an industry standard for 3D artists, and it is supported by both Windows and OS X (while 3D Studio Max, for example, isn't). You can check out 3D forums to delve into the differences between 3D software. The following is a list of very popular forums to begin with:

 

Setting up a scene in Maya


The first point to keep in mind when setting up a scene in Maya is that the standard unit in Maya is 1 cm, while the standard unit in Unity is 1 m So, whenever you export an FBX file from Maya into Unity, Unity scales it down to 0.01 percent of its original size.

Another very relevant point is that Maya and Unity are affected by strange kinds of idiosyncrasies that put them on opposing sides, with regard to what left or right and front or bottom mean. This is not something that only happens between Maya and Unity. Many 3D software disagree about the concepts of right and up. To get an idea, have a look at the following image, taken from Unity's forum:

As you can see, the red arrow, representing the left-right axis in the 3D world, may point to the left or right on different software or file formats, and the green and blue axes may switch to alternatively point to the forward or upward directions.

With Maya and Unity, what happens is that the front in Maya is the back in Unity. So you model the front of a character in Maya, and when you import it into Unity, it shows its back.

How do we deal with this? There's more than one option available, and turning the camera by 180 degrees in Unity is not the only one. We will show you how to deal with this problem but, before this, we need to learn how to actually export an FBX file from Maya and add it to a Unity scene, which is what we will do in the first recipe of this chapter.

Getting ready

For this recipe, we need a Maya scene with a 3D model, any model with at least one material applied to it. A textured model is provided with the contents of this book.

How to do it...

In this recipe, we will show you how a model is exported from Maya using the default FBX exporter panel and how the FBX file will get imported in Unity.

  1. Open your model in Maya.

  2. Open the outliner panel, and from the hierarchy, select the root node of your model. Remember that it is good practice to name the root node with a meaningful name, such as root. It can turn out to be useful, for example, when managing the exporting process through scripts (as shown later).

  3. Now, in the top menu window, navigate to File | Export Selection. The Maya exporter panel will open, as shown in the following screenshot. Don't bother with the panel on the right-hand side with the actual settings; we will get back to it in a while.

  4. Be sure that FBX export is selected from the drop-down menu at the bottom.

  5. Put a name you like in the File name field.

  6. Click on Export Selection to save the file in your destination folder.

  7. Now open Unity and, in the project panel, right-click and select Import New Asset…. from the menu:

  8. Select your saved FBX file from the Explorer window that opens and click on Import.

  9. Alternatively, you could have directly exported the FBX file from Maya into the Assets/Models directory of your Unity project.

  10. Now select the FBX file from the project panel and drag it onto the scene. The following screenshot shows what happens in Unity:

How it works...

The operation of exporting FBX files from Maya is simple: select the actual root node in the hierarchy and click on the Export button. But, as you can see, unless we use some precautions, the result of importing an FBX file from Maya is that the model is flipped by 180 degrees on the the y axis in Unity. This happens because the blue arrow that represents the z axis in Maya points in the opposite direction in Unity. As a consequence, the model shows its back to Unity's camera.

As we write, there are rumors that this issue is going to be solved in forthcoming Unity versions. For now, we will provide custom solutions we have used ourselves.

 

Using groups to rotate FBX files


An efficient solution to dealing with the discrepancy between Maya and Unity is to act on Maya's side and rotate the model on its y axis there. Though, as we write, this problem is going to be solved soon by Maya LT, we offer a solution here that prevents the imported FBX file from acting strangely once they are turned into prefabs in Unity. The idea is to use the so-called "groups" to apply the transformations required and yet get a clean hierarchy for the prefab to appear in Unity. Let's see how to do it.

Getting ready

Open the scene again with the model we used before and be ready to follow our instructions.

How to do it...

In this recipe, we will show you how to use groups and hierarchies in Maya to export a model that will not show its back once it gets imported into Unity. Have the Maya scene open on your screen.

  1. From the outliner panel, select the root node of your model. Be sure that the model is at the 0,0,0 position with 0,0,0 rotation.

  2. With root node selected, press Ctrl+G to create a group in the hierarchy.

  3. Double-click on the newly created group name to edit it and type rot_180 (this is actually just for reference so we know what the group means).

  4. Set a value, namely 180, for the rotation on the y axis in the Transform Attributes panel.

  5. With the rot_180 group selected in the hierarchy, press Ctrl+G again to create another group. Name this group export after double-clicking on the group name in the hierarchy.

  6. Now you can select the export node to export the selection in order to get an FBX file out of this model.

How it works...

By using one group for flipping the model on the y axis and another to make a selection featuring neither rotations nor translations for the export, we made sure that the FBX file won't have any unexpected rotation or position offsets that will affect its behavior once it gets scripted into the code in Unity.

There's more...

Another technique we will only mention here is to use your programming skills and code an AssetPostprocessor class to handle the process automatically.

AssetPostprocessor is a class in Unity, provided with several methods to act on the pipeline for importing assets into Unity.

What one could do is add a custom attribute to the model in Maya, something like turn me 180 degrees on the y axis when imported, and let the AssetPostprocessor class read this attribute and perform the transformation.

You can learn more about the AssetPostprocessor class by checking the scripting reference guide at http://docs.unity3d.com/ScriptReference/AssetPostprocessor.html.

 

Exporting FBX files from Maya


Now that we are done with our first import, we can approach the many settings available in the Maya FBX exporter panel. There are several operations that require our attention, so follow us in our next recipe.

Getting ready

For this recipe, you just need the Maya model we used so far.

How to do it...

  1. Open the scene with the model in Maya.

  2. From the outliner panel, select the root node of your model. Be sure that the model is at the 0,0,0 position with 0,0,0 rotation.

  3. In the top menu window, navigate to File | Export Selection and the Maya exporter panel will open.

  4. Be sure that FBX export is selected from the drop-down menu at the bottom of the panel.

  5. Put a name you like in the File name field.

  6. From the Options... panel on the right-hand side, let's examine the first group of settings. Edit the General Options, Reference Options, and Include Options tab, as shown in the following screenshot:

  7. Now we can move to the next group of settings. In File Type Specific Options, make sure that the Include and Geometry settings are configured as shown in the following screenshot:

  8. Next comes the animation-related group of properties. Since we are not importing animations with an FBX file, unflag the Animation option entirely. This action will disengage all the following properties (see that they are barred in the following screenshot).

  9. Unflag Cameras, Lights, and Embed Media; we don't need any of them either.

  10. Flag Input Connections in the Connections tab. Refer to the following screenshot for the last three steps:

  11. Finally, in the Advanced Options tab, check that Units is set to Automatic.

  12. Check that Axis Conversion is set to Up Axis: Y.

  13. Check that FBX File Format is set to the latest Maya version available for your Maya installation (Binary FBX 2014, as we write). Refer to the following screenshot to be sure you have set everything correctly:

  14. Set a destination directory for the export, most likely the Asset\FBX directory in your Unity project.

  15. You can now press Export Selection to have the FBX file saved and ready to be used in Unity.

How it works...

As you can see, there is quite a lot that can be tweaked with regard to exporting FBX files from Maya. What we offered here was a basic outline that will do for most cases. It is very likely that, depending on the specific need that would rise with your own project, you may need to use different settings on specific tabs. In such cases, we suggest you to refer to the official Unity documentation, where an entire section dedicated to exporting from Maya is available.

Anyway, at some point, you will have determined the optimal settings configuration for your project and won't need to reset them every time. Once you get your optimal configuration, the only change will be with regard to the Animation tab. Don't worry about it now; we'll get to importing animations soon.

There's more...

If you check Unity's reference manuals (http://docs.unity3d.com/Manual/HOWTO-ImportObjectMaya.html), you may learn that Unity actually imports native Maya files, which means you can directly open Maya scenes (*.mb and *.ma files) in Unity. You can therefore ask, Why export FBX files into Unity at all?

There is more than one reason actually; one that is worth $4,000 is that FBX files are far more shareable than native Maya files. For example, to open a Maya scene in Unity, you must have Maya installed or the file won't open. If you expect to exchange files between people on their own PCs, you cannot assume that each one of them will have a Maya license. It will thus be safer to use formats that don't require additional costly software, as is the case with Maya (almost $4,000 per license).

That said, it is still quite useful to open Maya scenes in Unity. For example, in Unity you can immediately check the result of modifications made on a file in Maya without the need to export an FBX file with each new edit. Also, as already stated, Maya LT is going to export FBX file directly into Unity.

 

Configuring imported FBX files in Unity


Whenever an FBX file is imported into Unity, it is possible to edit some of its properties using Unity Inspector. Usually, these are operations that are required for setting things such as the correct scale of the model, the materials settings, as well as animations and other animation-related settings.

The following recipe provides useful hints on correctly specifying these settings using Unity Inspector.

Getting ready

For this recipe, we will basically resume from where we left the previous recipe. After having exported the model from Maya, launch Unity. By default, Unity always opens up to the last project you worked on.

How to do it...

  1. From the FBX folder in your Assets directory, select the FBX file you just imported.

  2. In the Inspector panel, let's begin with the Model tab. Depending on the actual unit system you set Maya with, you may need to set Scale Factor for Unity. By default, Unity scales down imported FBX files to one-hundredth of their original size. If you didn't consider this when modeling the object in Maya, you may need to scale it up in Unity Inspector. In our case, we scale the model back to its original size, setting Scale Factor to 1.

  3. Set both Normals and Tangents to Import from the drop-down menu.

  4. Flag the Import Materials option.

  5. From the drop-down menu, set Materials Naming to From Model's Materials.

  6. For completeness, check that Material Search is set to Recursive-Up, which is the default setting.

  7. Click on the Apply button on the bottom-right corner. Check the following screenshot to ensure you did everything right:

  8. We can now move to the next tab, Rig. In the Avatar Definition field, select Create From This Model from the drop-down menu. This setting is important in order to animate the character. What we are stating here is that we want the Rig model for this character to be created from this actual FBX file.

  9. In the Root node field, set the root node of your model, then click on Apply. Check the following screenshot for reference. As stated previously, we took care of naming our root node root earlier:

  10. The last tab is called Animations. Since we are not importing the animations of our model with this specific FBX file, we will unflag the Import Animation option, as shown in the following screenshot:

  11. Click on Apply to update the setting.

How it works...

As already said with regard to the export settings in Maya, what we provided here was a default reference to set an FBX file imported in Unity. You may need to change some of these settings based on specific matters related to how you modeled your object in Maya and\or how you exported and\or planned to use it as a Unity game object.

We suggest you experiment with Inspector settings to get a wider grasp of the meaning of each setting and refer to the Unity documentation available at http://docs.unity3d.com/Manual/HOWTO-exportFBX.html when you have questions.

 

Exporting animations


It is very likely that models are enriched with animations to improve the quality of their behavior as game objects. In the following recipe, we will see how to export animations from Maya.

Getting ready

When exporting animations, two approaches are possible. One, which we will adopt in this book, is to keep things on separate files. With this approach, we export one FBX file for the model, the materials, and the rigging (as we did before), and one FBX file for each individual animation, such as idle, run, jump, and the like.

The other approach is to export everything on a single file. In this case, the exported FBX file will consist of the model, materials, rigging, and a timeline containing the frames for the entire animation set of the character. We will discuss this second approach later.

As usual, we took care of providing the required assets (animated Maya scenes for the model) in case you don't have any.

How to do it...

  1. With the root node of the animated model selected in the outliner, navigate to File | Export Selection as usual.

  2. Select FBX File Format in the exporting panel and name your file. If you want Unity to automatically read the animation name when it is imported, follow the official naming convention that requires the animation file to be named modelName@animationName (with @ before the animation name). Assuming that we are about to export the idle animation for our character, name the file modelName@idle.

  3. Select the destination directory for the Animation file, which should be under Animations in your Unity project.

  4. In the Options... panel, we need to make some adjustments to import the animation correctly. Under the Animation tab, flag the Animation option.

  5. In the Bake Animation field, check that Bake Animation is flagged and that the starting and ending frame for the current animation are selected (this should be done automatically by Maya).

  6. In the Deformed Models field, check that all the flags are selected. These settings may change, depending on the specific requirements of each individual model and animation set.

  7. You don't need to change anything else with regard to the setting we defined to export the static model, so you can click on Export Selection.

How it works...

As we will see in the following recipe, where we will edit the settings of the imported animation in Unity, what we get is an FBX file containing an animation clip named idle, which represents the idle animation for our character.

As we said before, it is possible to trace the animation clips on a single file\timeline and export all character animations in one FBX file; in this case, however, additional operations are required when compared to the one-file-one animation technique.

With a single file containing all the clips, Unity is not capable, by default, to read the timeline imported from Maya and automatically detecting the individual clips, and you end up with a single, most likely long, timeline named track01 containing all the animations.

So you have to split the whole timeline by yourself, naming each clip individually and manually setting the start and end frames for each one of them in Unity Inspector.

If you think about a typical working pipeline with different people taking care of different operations, you may find your animators manually writing a text file of some sort with information regarding animation clip names and their reference frames. The animators then pass on this text file to someone responsible for taking care of importing animations in Unity. This last guy would also be responsible for typing the data found in the text file into Unity Inspector. A way of doing things that can easily lead to human errors, as you can imagine...

This is the reason why we prefer using separate FBX files for each animation clip.

It is also possible to automate the exporting process so it doesn't become a time-consuming activity, but you may need a programmer for this.

The idea is to script a piece of code in MEL to handle the job. MEL is the scripting scaffold of Maya: any operation you perform in the Maya editor has an equivalent instruction in MEL. Since performing hardcoding in the MEL scripting language would go beyond the scope of this book, we just provide a few references here for those interested. The list of MEL exporting commands is available at http://download.autodesk.com/us/fbx/20112/Maya/_index.html.

An example MEL script, courtesy of James Kyle, is available at http://www.jameskyle.net/2013/03/maya-to-fbx-batch-export/.

There's more...

For those of you who are interested in automating the exporting process, there are ways, pretty elegant too, that require advanced programming skills.

One way is to create a Maya Embedded Language (MEL) script that reads the scene in Maya and exports what you need, based on the settings you define for the exporting process. MEL is the programming language behind Maya; any operation performed in Maya can be converted into a scripting instruction that will achieve exactly the same result. By using MEL, you can thus create a script that automatically exports animation clips into Unity on one or more FBX files, helping you save time (and reduce the risk of errors).

Another option is to configure Maya to generate an XML file that describes the animation data stored in the timeline of a model (animation names, starting\ending frames, and the like), and then read this XML file from Unity to automatically create the required FBX files.

Both these approaches are very similar to using the post-processor to read custom attributes from Maya, as we discussed earlier.

If you'd like to go that way, you can refer to the following links:

 

Configuring imported animations in Unity Inspector


Before we end this chapter about Maya and Unity, there is one last step we must take care of configuring imported animations in Unity Inspector. This is the topic of our next recipe.

Getting ready

Again, we pick up from where we left the previous lesson. Open Unity and select the animated FBX file in the project panel.

How to do it...

  1. With the animated FBX file selected in the project panel, go to Inspector and access the Model tab. Since we are only interested in the animation data stored in this file, we will basically unflag most of the options that we set when we imported the static model. Use the following screenshot as a reference and then click on Apply:

  2. Now we move on to the Rig tab. As the animation clip stored in this file is to be used by the model we imported before, we need to set the Avatar Definition field as Copy From Other Avatar.

  3. We also need to set the source avatar. Click on the small button to the right of the Source field and add the avatar created from the static model to it, as shown in the following screenshot:

  4. Click on Apply and select the last tab, Animations.

  5. In this tab, there is much to do. First of all, flag the Import Animations option, which will make a group of related options that depend on this option visible:

  6. Scroll down the panel and look for the Root Transform Rotation. Flag Bake Into Pose and select Root Node Rotation from the drop-down menu.

  7. In the Root Transform Position (Y) group, flag Bake Into Pose and check that Root Node Position (Y) is flagged. In the Root Transform Position (XZ) group, flag Bake Into Pose and ensure that Root Node Position isn't flagged.

  8. Notice that this is an idle animation and, as such, it should be set on looping by flagging the Loop Time option. We didn't do this intentionally to provide a more general example.

  9. Click on Apply to end configuring the animation.

How it works...

Let's begin with a few words on the topic of root motion, as Unity Inspector displays several options about it.

Root motion has to do with controlling the actual position of the mesh with regard to its collider while animations are being played. Most animations happen in place, meaning that the mesh and the collider don't actually move around as the character is animated. This may be the case for examples of walk and run animations.

There are other animations though that require the mesh and collider to actually move or rotate in the 3D world as the animation is played. Actions such as strafing, jumping, and other in-game specials require the mesh and the collider to change their position and rotation in the 3D world as the animation is performed.

By setting the properties of Root Rotation and Root Position groups, you control whether the collider should rotate or move with the mesh during specific animation clips.

In a walk animation, the start and stop root orientations and positions in world space are identical. In such cases where animations are acknowledged by Unity with a green light in Unity Inspector, it is recommended to flag the Bake into Pose option in the Root Transform Rotation group and set Root Node Rotation from the drop-down menu. Also, flag Bake Into Pose in the Root Transform Position (Y) group and set Root Node Position from the menu.

As for the Root Transform Position (XZ) group of options, the manual recommends using them for long, idle animations, where the repetition of many frames could lead the mesh to drift from its collider in the long run. In such cases, it is recommended that you bake the position on the x and z axes as well.

Latin speakers used to say "Repetita iuvant," meaning repetition helps. The settings provided here are to be intended as general, default settings that may not fit into any situation. For example, you may find yourself having to set Scale Factor in the Model tab for the animated FBX file too in order to prevent the animation from not fitting the model rig. Alternatively, you may need to tweak the Root Transform Rotation and Root Transform Position settings differently, depending on how you rigged\animated your model with specific animation clips. As usual, we recommend that you refer to the Unity documentation at http://docs.unity3d.com/Manual/AnimationsImport.html whenever you have questions.

There's more...

The Rig tab in Unity Inspector displays an option that we didn't mention here: Animation Type. You configure this option to select whether you want your character to be animated as a humanoid or a generic object, as it could be with a vehicle, a ball, or whatever.

Also, when a rigged FBX file is imported into a project, Unity automatically creates a so-called avatar out of it. This task is generally performed automatically by Mecanim, the built-in tool responsible for animation setting and management; we will discuss this in detail in Chapter 3, Animating a Game Character.

Mecanim has routines that examine the skeletal configuration of a rigged model imported in Unity and then recompute it into a general template it can interpret. This template is called the avatar. Once an avatar has been created, other animation clips can be targeted to that same avatar. This is how multiple animation clips are linked to a single character model in Unity.

In the recipe, we configured the model scale and material in the Model tab only for the static version of the model; in the Rig tab, we set the Avatar Definition option to Create for this model. For animated clips, on the other hand, we didn't configure the Model tab, and we set Avatar Definition to Copy From Other Avatar with a reference to the static avatar. This assumes Mecanim succeeds in interpreting the rigged model.

There are cases where for some reason Mecanim is unable to correctly compute the avatar; alternatively, you may just want to make changes on your own. In such cases, it is possible to manually edit the avatar in Unity Inspector and check that all bones required by Mecanim are correctly named and in place.

A recipe about setting up the avatar would go beyond the scope of this book, as it has a lot to do with modeling and rigging techniques, which are beyond the scope of this book.

Refer to the Unity manual at http://docs.unity3d.com/Manual/AvatarCreationandSetup.html. Besides this, if you are interested in delving more into this matter, we suggest the many tutorials available on websites about game development and 3D graphics. For example, digital tutors offer many exceptional tutorials about Unity in general and Mecanim in particular; we recommend you check them out.

About the Author
  • Claudio Scolastici

    Claudio Scolastici is an Italian game designer with a background as a researcher in the fields of psychology, artificial intelligence, and cognitive science.

    He is employed by Italian game developer SpinVector as Technical Game Designer and Unity specialist.

    In November 2013, he released a book titled Mobile Game Design Essentials for Packt Publishing, as well as tutorials about AI modeling and scripting at Digital-Tutors.

    Browse publications by this author
Latest Reviews (3 reviews total)
excellent book very concise and every easy to follow
Unity 2D Game Development Cookbook
Unlock this book and the full library FREE for 7 days
Start now