Stage 2 · Lesson 6 of 7
State machines
A treats an object as being in exactly one state at a time, such as idle, running, jumping, attacking or hurt. It also sets clear rules for moving from one state to another. This replaces a tangle of (true or false flags) like isJumping and isAttacking, which allow impossible combinations.
Start with an (a fixed list of named options) and a switch or match statement. Once the states grow, move to one class or node per state. Player controllers, enemy AI, menus and match flow all use state machines. Your engine's animation system is one too.
Common trapadding a new boolean for each new behaviour until the player can attack while dead or jump while in a menu.
A question to check yourself, an AI practice session and 1 cheat sheet entry come with the full roadmap. Unlock
Resources for this stage
Start with· follow one of these
- CourseFreeUnity Learn: Junior Programmer Pathway
Unity Technologies · Beginner
Unity's official free C# pathway, with small playable projects in every unit
- GuideFreeLearn C#
Microsoft · Beginner
Microsoft's free C# lessons, for the language itself when Unity code feels unfamiliar
Practise
Keep handy
Also worth your time
The exact order Unity calls Awake, Start, Update, FixedUpdate and the rest
- Game Programming PatternsGo deeper
Free book on the state, observer, component and other patterns games rely on