Creating Intelligent Enemies – FSMs

In this section, we'll define the code to work with the enemy prefab; specifically, the FSM defining its core behavior. The enemy, once spawned in the level, will enter chase mode, causing it to follow the player, wherever they may be. On reaching the player, the enemy will attack and cause damage.

The enemy AI is encoded in the BotAI.cs script file. See the following code sample:

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; public class BotAI : MonoBehaviour { public enum AISTATE { CHASE = 0, ATTACK = 1 }; public AISTATE CurrentState { get { return _CurrentState; } set { StopAllCoroutines(); _CurrentState = value; switch(_CurrentState) { case AISTATE.CHASE: ...

Get Unity 2018 By Example - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.