In this chapter, we'll expand our knowledge of the FSM pattern and its uses in games and learn how to implement it in a simple Unity game. We will create a tank game with the sample code that comes with this book. We'll be dissecting the code and the components in this project. The topics we'll cover are as follows:
Understanding Unity's state machine features
Creating our own states and transitions
Creating a sample scene using examples
Unity 5 introduced state machine behaviors, which are a generic expansion of the Mecanim animation states that were introduced in the 4.x cycle. These new state machine behaviors, however, are independent of the animation system, and we will learn to leverage these new features to quickly implement a state-based AI system.
In our game, the player will be able to control a tank. The enemy tanks will be moving around in the scene with reference to four waypoints. Once the player tank enters their visible range, they will start chasing us, and once they are close enough to attack, they'll start shooting at our tank agent. This simple example will be a fun way to get our feet wet in the world of AI and state FSMs.