Game Development Reference
In-Depth Information
28 }
29
30 //Wait until next frame
31 yield return null;
32 }
33 }
The following are the comments for code sample 7-12:
Line 17 : The Health-Seek state begins by finding the nearest medikit in
the scene and uses it for the agent destination. This is, in a sense, cheating,
because (of course) without remote viewing powers, the enemy should not
be able to know where the nearest medikit is. However, remember that what
matters is not what the enemy really knows but how it appears to the gamer.
If the gamer doesn't know about this logic and cannot learn about it from
appearances, then it would be of no significance. Also note that it's possible
for the player or another enemy to collect the medikit before the enemy
arrives at the destination. For this reason, on each frame, the enemy must
determine whether the destination medikit is still valid, and if not, they
must pick the next, nearest one.
Line 23 : If there is no medikit available or the health has been restored to safe
limits, the enemy would return to the Idle state.
The SeekHealth state demands that we find and retrieve a reference to the nearest
medikit in the scene. This is achieved using a GetNearestHealthRestore method,
as shown in the following code sample 7-13:
01 //Function to get nearest health restore to Target in scene
02 private HealthRestore GetNearestHealthRestore(Transform Target)
03 {
04 //Get all health restores
05 HealthRestore[] Restores =
Object.FindObjectsOfType<HealthRestore>();
06
07 //Nearest
08 float DistanceToNearest = Mathf.Infinity;
09
10 //Selected Health Restore
11 HealthRestore Nearest = null;
12
13 //Loop through all health restores
14 foreach(HealthRestore HR in Restores)
15 {
16 //Get distance to this health restore
 
Search WWH ::




Custom Search