Managing Character States with the State Pattern

In video games, entities continually transition from one state to another depending on player inputs or events. An enemy character might go from an idle state to an attack state depending on whether it spots the player moving across the map. A player character is constantly blending from one animation to another as it responds to the player inputs. In this chapter, we will review a pattern that permits us to define the individual states of an entity and its stateful behaviors.

To start, we will use the traditional State pattern to manage the individual finite states of our main character. In the context of our racing game project, the main character is a motorcycle, so it has a set of mechanical ...

Get Game Development Patterns with Unity 2021 - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.