FSM in Unity to Create Smart AI
Ok, let’s build a Finite State Machine that will act as our smart AI. As our AI travels the waypoints, we want it to go into attack mode at each point. For now we’ll just have it stop for 3 seconds and then move on to the next waypoint. Like this:
All we need is an If statement in our Attack Switch Case that calls a coroutine and updates the switch:
CalculateAIMovement is called from Update, and it checks to see if we’ve arrived at our waypoint, so let’s have it trigger the StateSwitch:
Now the AI will switch over to the Attack state and back to Walking after a 3 second pause.