Unity
2D Game Development Part-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:
·
ffSetting up a scene
in Maya
·
ffUsing groups to
rotate FBX files
·
ffExporting FBX
files from Maya
·
ffConfiguring
imported FBX files in Unity
·
ffExporting
animations
·
ffConfiguring
imported animations in Unity Inspector
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. Importing 3D Models and
Animations
16
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:
·
ffhttp://forums.cgsociety.org/
·
ffhttp://www.polycount.com/forum/
·
ffhttp://www.gameartisans.org/forums/forum.php
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.
1.
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).
2.
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.
1.
Be sure that FBX export is selected from the
drop-down menu at the bottom.
2.
Put a name you like in
the File name field.
3.
Click on Export Selection to save the file in
your destination folder.
4.
Now open Unity and, in
the project panel, right-click and select Import New Asset…. from the menu:
1.
Select your saved FBX
file from the Explorer window that opens and
click on Import.
2.
Alternatively, you
could have directly exported the FBX file from Maya into the Assets/Models directory of your
Unity project.
1.
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.
No comments:
Post a Comment