Game Development Reference
In-Depth Information
Lines 34 and 43 . Notice the additional member variable declarations that were
used in previously coded listings for accessing the NavMeshAgent component
and generating random destinations within the scene, for use with the
Patrol state.
Lines 160-183 . The AI_Chase coroutine defines the Chase state behavior. In
short, this function uses the Agent.SetDestination function to specify the Player
as the travel destination for the Enemy, since the Enemy must follow the Player.
The state will transition to another when the Player leaves the chase distance,
or when the Enemy is close enough to attack. The exact attack range for an
Enemy will vary, depending on the Enemy type. For example, Enemies with gun
weapons will feature a longer attack range.
Lines 187-221 . The AI_Attack coroutine handles the attack behavior for the
Enemy. From lines 211-219, the Enemy loops in an attack mode, and issues a
strike (or an attack), allowing for an intervening recovery or reload delay for its
weapon, for each strike. Each attack will invoke a Strike function call on the
object (in line 215), allowing each unique Enemy type to handle its own attack
differently, if required. It's generally good practice to make base classes as
abstract as possible, allowing derived classes to customize functionality as far
as needed.
Listing 7-10. Relevant Additions to the Enemy Drone Class
01 //------------------------------------------------
02 //Handle Chase State
03 public void Chase()
04 {
05 //Same animations as patrol
06 Patrol();
07 }
08 //------------------------------------------------
09 //Entered Attack State
10 public void Attack()
11 {
12 //Hide default and walk sprites
13 foreach(SpriteRenderer SR in WalkSprites)
14 SR.enabled=false;
15
16 //Hide default sprite
17 DefaultSprite.enabled = false;
18
19 //Entered attack state
20 SendMessage ("StopSpriteAnimation", ((int)ENEMY_STATE.PATROL),
SendMessageOptions.DontRequireReceiver);
21 SendMessage ("StopSpriteAnimation", ((int)ENEMY_STATE.ATTACK),
SendMessageOptions.DontRequireReceiver);
22 SendMessage("PlaySpriteAnimation", ((int)ENEMY_STATE.ATTACK),
SendMessageOptions.DontRequireReceiver);
23 }
24 //------------------------------------------------
 
Search WWH ::




Custom Search