Unity: Creating AI Behaviors Through A Finite State Machine

Michael Quinn
Unity Coder Corner
Published in
11 min readNov 7, 2022

--

In game development, artificial intelligence has become something to be expected of. Whether it is a companion or an enemy, the ability for the object to “think” on its own is something that can mystify people. This article is going to be a brief(as possible) explanation on how to achieve the perception of intelligence through a finite state machine.

The topics we are going to touch upon in this article are the State Pattern, interfaces, polymorphism, and lambda expressions.

This entire project can be found at https://github.com/M-Quinn/FiniteStateMachine

What Is A Finite State Machine

A finite sate machine is an abstract machine that can be in exactly one of a finite number of states at any given time.

In basic English, it is a a pattern we use that allows us to separate our logic so that only certain logic can be run based on whatever we want to base it on. For example, when I wake up, I make coffee. Waking up is the condition. Making coffee is the logic within the state.

Finite state machines are found in almost everything around us from video games, to business applications, to vending machines, and even traffic lights.

If you want to learn more about Interfaces and Polymorphism, check out my article Unity: The Magic of Polymorphism Through Interfaces.

Project Background

--

--

Michael Quinn
Unity Coder Corner

I’m a foodie and a software engineer. Unity and Unreal developer. See more at MikeQ.dev