Game Development Reference
In-Depth Information
This code breaks down as follows:
(1) private var contr : CharacterController;
Declares a reference variable
contr
of type CharacterController.
(2) private var moveVector : Vector3 = Vector3.zero;
Declares a reference variable
moveVector
of type Vector3 to hold the information on how far to move
the game object each frame.
(3) public var speed : float = 10.0f;
Declares a public float variable
speed
for adjusting the speed of the game object in the Inspector.
function Start () {
(4) controller = gameObject.GetComponent.<CharacterController>();
}
Accesses the CharacterController component reference and assigns it to controller.
function Update () {
(5) moveVector.x = Input.GetAxis("Horizontal") * speed;
(6) moveVector.z = Input.GetAxis("Vertical") * speed;
}
Here the input from the user is multiplied by the
speed
factor and stored in the respective x-axis and
z-axis components of the Vector3 variable
moveVector
.
(7) controller.Move(moveVector * Time.deltaTime);
First the
moveVector
information is multiplied by the
Time.deltaTime
smoothing factor, then passed
to the Controller Character
Move()
function.
The
Move()
function moves the game object, and returns information regarding any collisions from
the move in a
CollisionFlags
variable. This information can give you a general idea about where the
collisions occurred. The
CollisionFlags
variable is a bitmask, the explanation of which is beyond
the scope of this text. However, you can
+'
CollisionFlags
in MonoDevelop to open the Scripting
Reference and get an idea of how to filter
CollisionFlags
information (Figure
7-24
).