Game Development Reference
In-Depth Information
49
50 //If we have reached player then attack
51 if(Vector3.Distance(ThisTransform.position,
PlayerTransform.position) <= DistEps)
52 {
53 //We have reached distance, now attack
54 StartCoroutine(State_Attack());
55 yield break;
56 }
57
58 //Wait until next
59 yield return null;
60 }
61 }
The following are the comments for code sample 7-9:
Lines 17-48 : During this phase, the State loop determines that the player
visibility has been lost. When this happens, the enemy will continue to chase
the player for a period of ChaseTimeOut . After this time elapses, the enemy
checks for player visibility again. If the player is sighted at that time, the
chase resumes as it did earlier. Otherwise, the enemy changes to the Idle
state, ready to begin a new patrol in search of the player again.
Lines 51-59 : Here, the Chase state checks whether the enemy has come
within the attack range ( DistEps ). If so, the FSM would enter State_Attack .
Creating the Attack state
In the Attack state, the enemy continually attacks the player as long as they're
visible. After an attack, the enemy must recover before launching a new attack. The
only exit condition for this state is losing sight of the player. When this happens, the
enemy returns to the Chase state and, from there, they either go back to the attack
state or into Idle , depending on whether the player has been found, as shown in the
following code sample 7-10:
//This coroutine runs when object is in attack state
public IEnumerator State_Attack()
{
//Set current state
CurrentState = AI_ENEMY_STATE.ATTACK;
//Set Chase State
ThisAnimator.SetTrigger((int) AI_ENEMY_STATE.ATTACK);
 
Search WWH ::




Custom Search