Game Development Reference
In-Depth Information
The algorithm determines the projected cost of taking paths based on
the cost of getting to the next node and an estimate of getting from that
node to the goal. The estimation is performed by a heuristic function. The
term heuristic seems to be one of those funny words in AI that is difficult to
define. Alan Newell first defined it in 1963 as a computation that performs
the opposite function to that of an algorithm. A more useful definition of
its meaning is given by Russell and Norvig in Artificial Intelligence published
in 1995. They define a heuristic as any technique that can be used to
improve the average performance of solving a problem that may not
necessarily improve the worse performance. In the case of path finding, if
the heuristic offers a perfect prediction, that is, it can calculate the cost from
the current node to the destination accurately, then the best path will be
found. However, in reality, the heuristic is very rarely perfect and can only
offer an approximation.
Unity Hands On
Pathfinding with A*
Open the project and the scene patrolling . Programming the A*
algorithm is beyond the scope of this topic and has therefore been
provided with this project. If you are interested in the code, it can be
found in the Project in the Plugins folder.
Step 2. Locate the robot model in the Robot Artwork > FBX
folder in the Project. Drag the model into the Scene as shown
in Figure 5.7 . For the terrain and building models already in the
scene, the robot will need to be scaled by 400 to match. If the
textures are missing, find the material called robot-robot1 , which is
on the roothandle submesh of the robot; set it to Bumped Diffuse ;
and add the appropriate textures for Base and Normalmap . The
texture files will be in the Project. The model may also have a
subobject of the robot in the Hierarchy and unticking its Mesh
Renderer.
Step 3. Waypoints can be added to the scene in the same way
as they were in Chapter Three. Any GameObject can act as a
waypoint. Create 9 spheres and arrange them in a circuit around
the building. Add a 10th sphere out the front and an 11th sphere
in the driveway as shown in Figure 5.8 . Name the spheres Sphere1,
Sphere2, etc.
Step 4. Create a new JavaScript file called patrol and open it in the
script editor. Add the code shown in Listing 5.5 .