Game Development Reference
In-Depth Information
{
StopAllCoroutines();
Destroy(gameObject);
return;
}
//Check health danger level
if(Health > HealthDangerLevel) return;
//Health is less than or equal to danger level, now seek
health restores, if available
StopAllCoroutines();
StartCoroutine(State_SeekHealth());
}
The State_SeekHealth method can be coded, as shown in the following code
sample 7-12:
01 //This coroutine runs when object is in seek health state
02 public IEnumerator State_SeekHealth()
03 {
04 //Set current state
05 CurrentState = AI_ENEMY_STATE.SEEKHEALTH;
06
07 //Set Chase State
08 ThisAnimator.SetTrigger((int) AI_ENEMY_STATE.SEEKHEALTH);
09
10 //This is the nearest health restore
11 HealthRestore HR = null;
12
13 //Loop forever while in seek health state
14 while(CurrentState == AI_ENEMY_STATE.SEEKHEALTH)
15 {
16 //If health restore is not valid, then get nearest
17 if(HR == null) HR = GetNearestHealthRestore(ThisTransform);
18
19 //There is an active health restore, so move there
20 ThisAgent.SetDestination(HR.transform.position);
21
22 //If HR is null, then no more health restore, go to idle
23 if(HR == null || Health > HealthDangerLevel)
24 {
25 //Change to idle
26 StartCoroutine(State_Idle());
27 yield break;
 
Search WWH ::




Custom Search