Final Lecture 5
Final Lecture 5
Lecture 5
Working with 3D Characters
● For characters to animate properly in Unity, they must first be rigged inside 3D modeling
software, such as 3ds Max, Maya, or Blender. Rigged means that an underlying skeleton or
humanoid bone structure is added to the model by the artist, and each bone (such as the
arm bone, leg bone, and so on) is specifically weighted (magnetized) to the vertices of the
mesh. The purpose of the bones is to define the extent to which vertices in the character
mesh should move and deform to conform to the skeleton.
● We can use these applications to make our own models. Besides, many 3D models are
available for download from game art websites. One great resource for 3D models is the
Unity Asset Store. Unity supports multiple 3D file formats. However, COLLADA and FBX
are the best two options to choose.
2
Importing Rigged Characters
● Importing rigged characters into Unity
is initially a drag-and-drop operation.
Model 3 3
Importing Rigged Characters
● In the import settings for the model:
1) Scale Factor in the Model tab: identifies the size of the model. A
scale factor of 1.0 means that the model will appear at its original
size, unless it’s being scaled in the scene. The value 0.01 means
that the model will appear in your scene 100 times smaller than
its original size.
● If we move the player by directly changing its transform position, the character wouldn’t
detect collisions (the player would pass through everything).
8
CharacterController Component
● Move ( Vector3 Motion ) This method attempts to move the controller by Motion. The
motion will only be constrained by collisions. The movement values should be muliplied by
Time.deltaTime to get frame rate–independent movement (the character moves at the same
speed on different computers with different frame rates).
● CharacterController includes isGrounded property that can be used for checking whether
the character is on the ground or not; this value is true if the bottom of the character
controller collided with anything in the last frame.
9
CharacterController Component
● Create a new C# script and
name it CharacterMovement,
and attach it to the character
(Player) Gameobject, and write
the following code:
10
CharacterController Component
● Using isGrounded property for detecting the ground works the majority of the time.
● However, when the character stepping off edges, we will probably notice that the character
seems to float in the air. That’s because the collision area of the character is a surrounding
capsule and the bottom of this capsule will still be in contact with the ground when the
player steps off the edge of the platform. Similarly, if the character stands on a slope, the
current ground detection will cause problematic behavior.
11
CharacterController Component
● Thus, checking for collisions on the bottom of the character isn’t a great way to determine
whether the character is on the ground. Instead, the raycasting approach is used to detect
the ground.
● It is the process of casting a ray straight down from the player’s position. If it registers a hit
just below the character’s feet, the player is standing on the ground.
12
Animation
● The imported characters (3D models) have arms stuck straight out to each
side, rather than the more natural arms-down pose. That arms-out position
is referred to as the T-pose, and the standard is for animated characters to
default to a T-pose because animations haven’t been applied yet.
● The movement scripts make the character walkes around in the scene
correctly by changing the overall position of the character. However, the
character will be stuck in a T-pose.
● Therefore, we need to animate the character out of the T-pose. That is,
animation sequences are to be assigned to the character to make the
detailed movements of feet hitting the ground, arms and legs swinging
back and forth, etc. 13
Animation System
● Unity has a sophisticated animation system called Mecanim, which allows developers to
build animations for characters.
● It is designed so that you can visually set up a complex network of animations for a
character and then control those animations with a minimum of code.
● The heart of the animation system is composed of two kinds of assets: animation clips and
animator controllers.
14
Animation Clips
● A lifelike character can make different movements at different times: sometimes the player
is running around, sometimes the player is jumping on platforms, and sometimes the
character is just standing there with its arms down.
● The first step in setting up animations for a character is defining the various animation clips
that will be played.
● Animation clips are one of the core elements to Unity’s animation system.
● Unity supports importing animation from external sources, and offers the ability to
create animation clips from scratch within the editor using the Animation window.
15
Animation Clips
● Animation clips imported from external sources could include:
1) Humanoid animations captured at a motion capture studio.
3) Animation sets from 3rd-party libraries (eg, from Unity’s asset store).
16
Animator Controller
● The Mecanim system enables animators and programmers to organize Animation Clips into
a structured flowchart-like system called an Animator Controller.
● The Animator Controller acts as a state machine diagram where the states in the diagram
are different Animation clips that could be playing.
● The controller manages the various animation states and the transitions between them,
which could be thought of as a simple program written in a visual programming language
within Unity.
● A very simple Animator Controller might only contain one or two clips, for example to
animate a door opening and closing at the correct time.
17
Animator Controller
● A more advanced Animator Controller might contain dozens of humanoid animations for
all the main character’s actions, and might blend between multiple clips at the same time to
provide a fluid motion as the player moves around the scene.
● After creating animation clips, the next step is to create the animator controller for the
character. This step allows us to set up animation states and create transitions between those
states.
● Various animation clips are played during different animation states, and then scripts will
cause the controller to shift between animation states.
18
Animator Controller
● To create a new animator
controller asset:
A series of parameters (number or Boolean values) can be created here to control the
animations. The currently active state transitions between states on the graph when
these values change. 21
Animator Controller
4) Import Idle, Walk, Run, and Jump
animations into Unity project. Drag
and drop these animations into your
project panel.
23
Animator Controller
7) Link the nodes together with lines
indicating transitions between animation
states: Right-click a node and select Make
Transition to start dragging out an arrow
on another node to connect. Connect nodes
in the pattern shown here. Be sure to make
transitions in both directions for most
nodes. These transition lines determine
how the animation states connect to each
other and control the changes from one
state to another during the game.
24
Animator Controller
8) Create parameters to control the
transitions: Click + button in the
Parameters tab Add a float called
Speed and a Boolean called Jumping.
Those values will be adjusted by
code, and they’ll trigger transitions
between animation states.
25
Animator Controller
9) Click the transition lines to see their
settings in the Inspector and adjust the
conditions of these transitions.
26
Animator Controller
■ Click the Idle-to-Run transition
Add a condition Set it to
Speed > 1.05.
27
Animator Controller
■ Click the Walk-to-Run transition
Add a condition Set it to
Speed > 1.05.
28
Animator Controller
■ Click the Idle-to-Jump transition
Add a condition Set it to
Jumping is true.
29
Animator Controller
■ Click the Run-to-Jump transition
Add a condition Set it to Jumping is
true.
31
Animator Controller
13) Create a new C# script and
name it CharacterMovement,
attach it to the character
(Player) Gameobject, and
write the following code:
32
Sliding Door
1) Create three Cubes GameObjects in the
scene and name it LeftWall, RightWall, and
TopWall.
34
Sliding Door
6) Type LeftDoor in the File name
Press Save. An Animator controller
and Animation Clip with the name
LeftDoor are created.
35
Sliding Door
8) Click the record button in the top left
of the Animation Window. When the
record button is on, it will take almost
any change you make to the game and
save it as part of the Animation.
36
Sliding Door
10) At keyframe 60, change the x position
of the LeftDoor GameObject such that
it is opened. This new position will be
baked into the Animation Clip named
LeftOpen.
37
Sliding Door
11) Create a new Animation
Clip and name it
LeftClose.
38
Sliding Door
12) Select LeftClose Animation Clip
Click the record button in the top left
of the Animation Window.
39
Sliding Door
14) At keyframe 60, change the x position
of the LeftDoor GameObject again such
that it is closed (returns it back to its
original position).
40
Sliding Door
15) Select LeftOpen and LeftClose
Animation Clips separately
Uncheck Loop Time in the Inspector
panel.
41
Sliding Door
16) Open the Animator view: Select the
created LeftDoor animator controller in
the Project panel Choose Window
select Animation select Animator
from this menu.
43
Sliding Door
20) Connect nodes in the pattern
shown here. Make a transition
from LeftIdle node to LeftOpen
node, a transition from
LeftOpen node to LeftClose
node, and a transition from
LeftClose node to LeftOpen
node.
44
Sliding Door
21) Click the transition lines and adjust the
conditions of these transitions:
46
Sliding Door
24) Create a new empty
Gameobject and name it
Door which is considered
as a game manager to
which we will assign the
script. Add a BoxCollider
component to this object
and increase its size.
Check Is Trigger property
so that other colliders will
simply pass through it.
47
Sliding Door
25) Create a Player
Gameobject.
48