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: StartCoroutine(StateChase()); break; case AISTATE.ATTACK: StartCoroutine(StateAttack()); break; } } } [SerializeField] private AISTATE _CurrentState = AISTATE.CHASE; private NavMeshAgent ThisAgent = null; private Transform ThisPlayer...