How it works...

Each state is a MonoBehaviour script that is enabled or disabled according to the transitions it comes from. We take advantage of the LateUpdate function in order not to change the usual train of thought when developing behaviors, and we use it to check if it is time to transition to a different state. It is important to disable every state in the game object, apart from the initial one.

The following diagram shows the graphical representation for a finite-state machine.

A finite-state machine representation

Get Unity 2018 Artificial Intelligence Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.