Game Development Reference
In-Depth Information
also spend a lot of time placing waypoints all over the map as a solution.
However, a simpler solution is to place temporary waypoints down on the
map that draw out the path the player is walking and have the NPC follow
them instead. This is another common technique used in games called
breadcrumb pathfinding . To develop a simple breadcrumb script, create a
new JavaScript file called breadcrumbs and add the code in Listing 5.12 .
Listing 5.12 Recording Player Movement with Breadcrumbs
var breadcrumbs:Array = new Array();
private var lastPos:Vector3;
function RemoveBreadCrumb()
{
Destroy(breadcrumbs[0]);
breadcrumbs.RemoveAt(0);
}
function Update ()
{
if(lastPos != this.transform.position)
{
var bc =
GameObject.
CreatePrimitive(PrimitiveType.Sphere);
bc.transform.position = this.transform.
position;
Destroy(bc.collider);
bc.transform.localScale = Vector3(0.5,0.5,0.5);
bc.renderer.material.color = Color.green;
breadcrumbs.Add(bc);
}
lastPos = this.transform.position;
if(breadcrumbs.length > 100)
{
RemoveBreadCrumb();
}
}
Step 18. Attach this script to the First Person Controller.
Step 19. Play. Walk backward and look at the green spheres being added
to the map. These are your breadcrumbs. They mark the last 100 locations
your First Person Controller was on the map. Of course, in a real game you
wouldn't see these but they are added here for illustrative purposes.
Step 20. To get the NPC to follow these breadcrumbs when it is in pursuit,
modify the patrol script as shown in Listing 5.13 .
Search Nedrilad ::




Custom Search