0% found this document useful (0 votes)
15 views49 pages

Final Lecture 5

The document discusses setting up 3D character animations in Unity. It describes how 3D characters must be rigged in external software before being imported into Unity. It also discusses applying animation clips and an animator controller to play different animations, such as walking, jumping, or standing. The animator controller acts as a state machine that manages transitions between animation states. Character movement is handled using the CharacterController component to constrain movement to collisions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views49 pages

Final Lecture 5

The document discusses setting up 3D character animations in Unity. It describes how 3D characters must be rigged in external software before being imported into Unity. It also discusses applying animation clips and an animator controller to play different animations, such as walking, jumping, or standing. The animator controller acts as a state machine that manages transitions between animation states. Character movement is handled using the CharacterController component to constrain movement to collisions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Unity

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.

● To import a 3D model into Unity: Drag


the FBX file from the computer into
Unity’s Project view or right-click in
Project  choose Import New Asset. Model 1
Model 2
● The 3D model will be copied into the
Unity project and show up ready to be
put in the scene.

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.

2) Normals option in the Model tab: controls how lighting and


shading appear on the model, using a 3D math concept known
as, well, normals. The default setting for Normals is Import,
which will use the normals defined in the imported mesh
geometry. 4
Importing Rigged Characters
3) Animation Type in the Rig tab: By default, this value will
probably be Generic if the mesh contains rigging information,
or None if the mesh has no rigging information. Generic
should be used for rigged noncharacter meshes, such as
cranes, three-headed monsters, snakes, trees, and others.

Select Humanoid, if it's not already activated. Humanoid is


the recommended setting to choose for human characters
because it gives us access to the entire range of animation
features in Mecanim.

4) Click the Apply button in the Inspector.


5
CharacterController Component
● CharacterController is a built-in Unity component designed to provide movement and
collision detection for a character.

● If we move the player by directly changing its transform position, the character wouldn’t
detect collisions (the player would pass through everything).

● CharacterController is a component that can be used to easily do character movement


constrained by collisions without having to deal with a rigidbody. That is, it makes the
object move more like a character in a game, including colliding with walls.

● CharacterController component is used only for 3D objects.

● For 2D objects, a Rigidbody component is used where the movement is applied to


Rigidbody’s velocity, rather than something like position. 6
CharacterController Component
● To attach a CharacterController component to the Player
GameObject: Select the Player in the Hierarchy  click the
Add Component button in the Inspector window  type
“Character Controller” into the search bar and press Enter.

● The CharacterController uses a capsule shape to test for


collisions. This is represented in the Scene window as a
wireframe set of green lines. That’s how big the player is
perceived to be with the current settings of the controller.

● Center: identifies the center of the character's capsule


relative to the transform's position.
7
CharacterController Component
● Height: identifies the height of the character's capsule.

● Radius: identifies the radius of the character's capsule.

● Change the values of Center, Radius, and Height so that the


collider mostly cover the Player, while the rest of the
settings can be left as is.

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.

● Each movement is a separate animation clip that can play individually.

● 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.

2) Animations created from scratch by artists or animators in an external 3D application


(such as Autodesk, 3ds Max, or Maya).

3) Animation sets from 3rd-party libraries (eg, from Unity’s asset store).

4) Multiple clips cut and sliced from a single imported timeline.

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:

1) Right click in the Project


panel  select Create 
Animator Controller.

2) In the Project view, an icon


with a funny-looking
network of lines on it is
appeared. Rename this
asset Player.
19
Animator Controller
3) Open the Animator view:
Select the created
animator controller in the
Project panel  Choose
Window  select
Animation  select
Animator from this menu.

The Animator view is


opened and displays
animator controller that is
currently selected. 20
Animator Controller
Initially, there are only two default nodes in
the Animator view, for Entry and Any State.
Any State node will not be used. Instead, we
will drag in animation clips to create new
nodes.

Each node on the graph is an animation state.


The named animation clip plays when the
controller is in that state.

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.

5) Drag the imported animation clips


from the Project panel into the
Animator view. The orange node is
the default animation state which is
where the network of nodes starts
before the game has made any change
(before any transitions happen). 22
Animator Controller
6) Make Idle node the default
animation: Right-click the Idle node
 select Set as Layer Default State.
That node will turn orange while
the other nodes stay gray.

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.

■ Click the Idle-to-Walk transition 


Add a condition  Set it to
Speed > 0.05.

■ Click the Walk-to-Idle transition 


Add a condition  Set it to
Speed < 0.05.

26
Animator Controller
■ Click the Idle-to-Run transition 
Add a condition  Set it to
Speed > 1.05.

■ Click the Run-to-Idle transition 


Add a condition  Set it to
Speed < 0.05.

27
Animator Controller
■ Click the Walk-to-Run transition 
Add a condition  Set it to
Speed > 1.05.

■ Click the Run-to-Walk 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.

■ Click the Jump-to-Idle transition 


Add a condition  Set it to
Jumping is false.

29
Animator Controller
■ Click the Run-to-Jump transition 
Add a condition  Set it to Jumping is
true.

■ Click the Walk-to-Jump transition 


Add a condition  Set it to Jumping is
true.

10) Uncheck Has Exit Time for all transitions.


With this enabled the transition would go
through the whole animation before
transitioning. Which means there will be
a delay if the player presses the walk key. 30
Animator Controller
11) Add Animator component to the character:
Select the Character in Hierarchy window 
click Add Component in the Inspector
window  select Animator.

12) Attach Animator Controller to the Animator


component of the Character: Select the
Animator component in the Inspector
window  click the circle dot icon  choose
the Animation Controller.

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.

2) Create two Cubes GameObjects in the scene


and name it LeftDoor and RightDoor.

3) Add Animator component to the left and


right door: Select each door separately in
the Hierarchy view  Click Add
Component in the Inspector panel 
choose Animator.
33
Sliding Door
4) Select LeftDoor in the Hierarchy
view  Click the Window menu 
Select Animation  Select
Animation again.

5) Click Create button in the opened


Animation window.

34
Sliding Door
6) Type LeftDoor in the File name 
Press Save. An Animator controller
and Animation Clip with the name
LeftDoor are created.

7) Rename the created Animation Clip


to LeftOpen.

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.

9) At keyframe 0, keep the LeftDoor close.

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.

The animation system will


automatically interpolate between the
two states (slowly transition from close
to open). The interpolation is linear by
default.

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.

13) At keyframe 0, change the x position of


the LeftDoor GameObject such that it is
opened.

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.

Loop refers to a recording that plays


over and over repeatedly. A looping
animation clip is one that plays again
from the start as soon as playback
reaches the end.

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.

The Animator view is opened and


displays animator controller that is
currently selected.

17) Add an empty state: Right click on the


Animator view  Select Create State
 Select Empty. 42
Sliding Door
18) Rename the created
empty state to LeftIdle
and Set it as Layer
Default State.

19) Create a Boolean


parameter called
Opened to control the
transitions.

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:

■ LeftIdle-to-LeftOpen transition  Add a


condition  Set it to Opened is true.

■ LeftOpen-to-LeftClose transition  Add


a condition  Set it to Opened is false.

■ LeftClose-to-LeftOpen transition  Add


a condition  Set it to Opened is true.

■ Uncheck Has Exit Time for all


transitions.
45
Sliding Door
22) Attach the LeftDoor Animator
Controller to the Animator
Component of the LeftDoor
GameObject using a simple
drag-and-drop operation (If it is
not already attached to it).

23) Repeat from step (4) to step (22)


for the RightDoor GameObject.

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.

26) Create a new C#


script and name it
SlidingDoor, attach
it to the Door
Gameobject, and
write the following
code:

48

You might also like