Attacking the player
The third and final state for the NPC is the attack state, during which the chick will periodically attack the player (or at least pretend to attack the player, as you'll see shortly).
This state can only be reached from the chase state. During a chase, the chick will check whether they are within attacking distance. If so, the chase state will request that the FSM change from chasing to attacking. If, during an attack, the player retreats, then the chick will change from attacking to chasing.
The attack state will have a related animation, as we alluded to in Chapter 7, Creating Artificial Intelligence, when we created the animations for the chick; however, before we create the custom animations, we'll write the state's implementation.
Implementing the state
As with the previous two states, the attack state is contained in a separate class:
- Create an
AttackState
class:public class AttackState : MonoBehaviour, IFSMState { ...