Game Development Reference
In-Depth Information
Listing 5.13 Adding Breadcrumb Following Code to the NPC
...
else if(state == "pursue")
{
if(prevState != state)
{
...
}
else if(event == "update")
{
position =
target.gameObject.
GetComponent("breadcrumbs").
breadcrumbs[0].
transform.position;
if(Vector3.Distance(position,this.transform.
position) < 2)
{
target.gameObject.
GetComponent("breadcrumbs").
RemoveBreadCrumb();
}
direction = position - transform.position;
direction.y = 0;
...
}
...
Step 21. Play. When the NPC is in pursuit of the player it will follow
the breadcrumb trail. As it reaches a breadcrumb it deletes it from the
player's breadcrumb array.
Step 22. Last but not least, we want the NPC to continue patrolling
when the player manages to move beyond its range. To do this we
need to set the state to idle and also find the NPC its closest waypoint.
That way the A* algorithm can work to set the NPC back on its patrol
path. To do this, modify the patrol script to that in Listing 5.14 .
Listing 5.14 Finding the Closest Waypoint to Set NPC
Back to Patrol
private var graph: Graph = new Graph();
...
function CanShootTarget(): boolean
{
...
}
Search Nedrilad ::




Custom Search