The Attack state
The third and final state for the NPC is the Attack state, during which the NPC is continually attacking the player. This state can only be reached from the Chase state. During a chase, the NPC must determine whether they have reached within attacking distance. If so, the NPC must change from chasing to attacking. If, during an attack, the player leaves the attacking distance, then the NPC must change from attacking to chasing. Consider the following code sample, which includes the complete EnemyAI
class, with all coded and completed states:
using UnityEngine; using System.Collections; //------------------------------------------ public class AI_Enemy : MonoBehaviour { //------------------------------------------ public enum ENEMY_STATE {PATROL, CHASE, ATTACK}; //------------------------------------------ public ENEMY_STATE CurrentState { get{return currentstate;} set { //Update current state currentstate = value; //Stop all running...