Game Development Reference
In-Depth Information
Listing 5.5 Initializing Waypoints in a Graph Object
var waypoints: GameObject[];
//create a new graph to process waypoints
var graph: Graph = new Graph();
var currentWP: int = 0;
private var speed: int = 8;
private var rotationSpeed: int = 5;
private var accuracy: Number = 1.0;
function Start ()
{
if(waypoints.length > 0)
{
// add all the waypoints to the graph
for(var i = 0; i < waypoints.length; i++)
{
graph.AddNode(waypoints[i], true, true);
}
//create edges between the waypoints
graph.AddEdge(waypoints[0], waypoints[1]);
graph.AddEdge(waypoints[1], waypoints[2]);
graph.AddEdge(waypoints[2], waypoints[3]);
graph.AddEdge(waypoints[3], waypoints[4]);
graph.AddEdge(waypoints[4], waypoints[5]);
graph.AddEdge(waypoints[5], waypoints[6]);
graph.AddEdge(waypoints[6], waypoints[7]);
graph.AddEdge(waypoints[7], waypoints[8]);
graph.AddEdge(waypoints[8], waypoints[0]);
}
}
function Update()
{
//draw the paths in the scene view of the editor
//while playing
graph.debugDraw();
}
Step 5. Attach the patrol script to the robot in the Hierarchy. Add the
waypoints in order to the Waypoints array of the patrol script as shown in
Figure 5.9 .
Step 6. Play. While playing, switch to the Scene. The code in the Update
function will draw lines along the edges. The blue tip indicates the
Search Nedrilad ::




Custom Search