Game Development Reference
In-Depth Information
Figure 7-14. The navigation mesh marks out walkable space in the level
The Patrol State
In the Patrol state, the Enemy will wander the scene, traveling from its current position to a randomly
selected destination elsewhere in the level. Achieving this requires some amendment to the Enemy
class. Let's see some of the changes to the AI_Patrol coroutine, as shown in Listing 7-7. In-depth
comments follow.
Listing 7-7. Updating the Patrol State Coroutine
01 //------------------------------------------------
02 //AI Function to handle patrol behaviour for enemy
03 //Can exit this state and enter chase
04 IEnumerator AI_Patrol()
05 {
06 //Stop Agent - NavMeshAgent - declared as a member of the class. See Code 7-9
07 Agent.Stop();
08
09 //Loop forever while in patrol state
10 while(ActiveState == ENEMY_STATE.PATROL)
11 {
12 //Get random destination on map
13 Vector3 randomPosition = Random.insideUnitSphere * PatrolDistance;
14
15 //Add as offset from current position
16 randomPosition += ThisTransform.position;
17
18 //Get nearest valid position
19 NavMeshHit hit;
20 NavMesh.SamplePosition(randomPosition, out hit, PatrolDistance, 1);
21
 
Search WWH ::




Custom Search