Game Development Reference
In-Depth Information
Figure 7-23. The Character Controller component in the Inspector
To test some of these properties you will need to populate the scene with a few more items. In the
Project panel, select the Assets ➤ Sample Assets ➤ Prototyping ➤ Prefabs folder and add a Loop
to the scene. Give the Loop a y-rotation of 90. The Loop and Plane should each have Transform
positions of (0, 0.5, 0) while the Cube should have a Transform position of (0, 0, 0). Give the Plane
a scale of (20, 20, 20), and add a little contrast by dragging the prototype_grey_dff texture from the
Assets ➤ Sample Assets ➤ Prototyping ➤ Prefabs ➤ Textures folder and dropping it on the Plane
in the Scene view. Drill down one more folder into Compound Prefabs and drag a Box Pile into the
scene; give it a Transform position of (2, 0, -8).
Finally, in the Assets ➤ Standard Assets ➤ Scripts ➤ Camera Scripts folder, find the Mouse Orbit
script. If the folder isn't there, in the top menu select Assets ➤ Import Package ➤ Scripts to obtain
it. Drag and drop it onto the Main Camera. Select Main Camera in the Hierarchy. Drag the Cube
game object from the Hierarchy to the Main Camera ➤ Mouse Orbit Script component and drop it
into the Target property field.
The scene is set up and the Character Controller properties are set. Now for a script to get the Cube
game object moving. In the Project panel select the Assets ➤ Scripts folder, then Create ➤ New
Script ➤ Javascript. Name it CCScript and open it in MonoDevelop. Edit the code to the following:
#pragma strict
private var contr : CharacterController;
private var moveVector : Vector3 = Vector3.zero;
public var speed : float = 10.0f;
function Start () {
contr = gameObject.GetComponent.<CharacterController>();
}
function Update () {
moveVector.x = Input.GetAxis("Horizontal") * speed;
moveVector.z = Input.GetAxis("Vertical") * speed;
contr.Move(moveVector * Time.deltaTime);
}
 
Search WWH ::




Custom Search