Game Development Reference
In-Depth Information
Character Controller
You've moved game objects with scripts, animations, and physical forces. You have spent some
time using Unity's Sample Assets controllers like the Third Person Character, but you can also make
your own controller using the Character Controller component. For first-person or third-person
characters that you do not want affected by rigidbody physics, the Character Controller component
may be just the thing.
In the editor, create a new scene, and add a Plane and a Directional light. In the Hierarchy select
Create ➤ Cube. Notice that the primitive game object Cube does not have a Rigidbody component.
Recall that rigidbody movement is controlled by the physics engine, so a Character Controller
component also trying to control movement and a Rigidbody component on the same game object
would result in odd behavior.
Notice that the Cube game object already has a Box Collider. The Character Controller is essentially
a Capsule Collider whose movement can be controlled by a script. It inherits from the Collider
class, which means that it contains the Collider class capabilities and builds on them with more
customized functions. With the Cube game object selected in the Hierarchy, in the Inspector select
Add Component ➤ Physics ➤ Character Controller. In the Scene view, you'll see the Capsule
Collider gizmo appear within the Cube game object (Figure 7-22 ).
Figure 7-22. The Character Controller Capsule Collider within the Cube game object
In the Inspector you will find the Character Controller. The controller cannot climb a slope steeper
than the Slope Limit . The Step Offset is a similar vertical constraint that determines how high
from the ground the controller can climb stairs. Skin Width sets the margin within the surface that
another collider can penetrate into the controller's collider before detecting a collision. A general
rule of thumb is 10% of the character controller's radius, so change this to 0.1. To reduce jitter,
if the controller is told to move a distance less than the Min Move Distance , it will stay in place. The
higher the number, the more control input required. Like a standard collider, the Capsule Collider can
be offset using Center , and adjusted in size using Radius and Height (Figure 7-23 ).
 
Search WWH ::




Custom Search