The Chase state
The Chase state is the second of the three in the enemy FSM. This state connects to both the Patrol and Attack states directly. It can be reached in one of two ways. If a patrolling NPC establishes a direct line of sight to the player, then the NPC changes from patrolling to chasing. Conversely, if an attacking NPC falls outside the reach of the player (perhaps because he is running away), the NPC resorts to chasing again. From the chasing state itself, it's possible to move either to the Patrol or Attack state, with the inverse conditions as those, which lead to chase. That is, if the NPC loses sight of the player, it returns to patrolling and, if the NPC reaches within attacking distance of the player, it switches to attacking. Consider the following code sample, which amends the AIEnemy
class to support the chase behavior:
public IEnumerator AIChase() { //Loop while chasing while(currentstate == ENEMY_STATE.CHASE) { //Set loose search ThisLineSight...