Game Development Reference
In-Depth Information
Step 5. The current code does not take into consideration the
direction the NPC is facing. If you approach it from behind, it will still
sense your presence when you are close enough. For it to use a field
of vision angle, modify the AI script as shown in Listing 5.2 .
Listing 5.2 Restricting the Field of Vision of an NPC to an
Area in Front of It
var target : Transform;
//the enemy
...
private var keepDistance = 10.0; //closest distance to get to enemy
private var rotationSpeed = 5.0;
private var sightAngle = 30;
private var speed = 0.01;
private var state = "PATROL";
function Start()
...
function CanSeeTarget () : boolean
{
var directionToTarget = target.position − transform.
position;
var angle =
Vector3.Angle(directionToTarget, this.
transform.forward);
if (Vector3.Distance(transform.position, target.position) >
seeRange || angle > sightAngle)
return false;
return true;
}
Step 6. Play. You will now be able to sneak up behind the NPC without
it noticing you.
5.3 Graph Theory
It would be impossible to begin talking about the ins and outs of AI
without a brief background in graph theory. Almost all AI techniques
used in games rely on the programmers having an understanding of
graphs. A graph in this context refers to a collection of nodes and edges.
Nodes are represented graphically as circles, and edges are the lines that
connect them. A graph can be visualized as nodes representing locations
and edges as paths connecting them. A graph can be undirected, which