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 ...
Get Unity 2018 By Example - Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.