Game Development Reference
In-Depth Information
180 //Wait until next frame
181 yield return null;
182 }
183 }
184 //------------------------------------------------
185 //AI Function to handle attack behaviour for enemy
186 //Can exit this state and enter either patrol or chase
187 IEnumerator AI_Attack()
188 {
189 //Stop Agent
190 Agent.Stop();
191
192 //Elapsed time - to calculate strike intervals
193 float ElapsedTime = RecoveryDelay;
194
195 //Loop forever while in chase state
196 while(ActiveState == ENEMY_STATE.ATTACK)
197 {
198 //Update elapsed time
199 ElapsedTime += Time.deltaTime;
200
201 //Check distances and state exit conditions
202 float DistanceFromPlayer = Vector3.Distance(ThisTransform.position,
PlayerTransform.position);
203
204 //If outside chase range, then revert to patrol state
205 if(DistanceFromPlayer > ChaseDistance) {ChangeState(ENEMY_STATE.PATROL);
yield break;}
206
207 //If within attack range, then change to attack state
208 if(DistanceFromPlayer > AttackDistance) {ChangeState(ENEMY_STATE.CHASE);
yield break;}
209
210 //Make strike
211 if(ElapsedTime >= RecoveryDelay)
212 {
213 //Reset elapsed time
214 ElapsedTime = 0;
215 SendMessage("Strike",SendMessageOptions.DontRequireReceiver);
216 }
217
218 //Wait until next frame
219 yield return null;
220 }
221 }
222 }
223 //------------------------------------------------
Search WWH ::




Custom Search