How to implement Finite State Machine in Unity

Published: 27 April 2022
on channel: PitiIT
15,736
385

In this Unity Scripting tutorial we will learn about achieving separation of concerns using Finite State Machine pattern. We will see how separating code to different classes (states) will allow us to provide better maintenability and a little bit cleaner code. It's one of those design patterns that if you fall in love with you won't be able to stop using. Don't be scared by the amount of setup code - this is really worth it as it allows you to write less code (and better code!) later on.

Discord:   / discord  

Code: https://github.com/x1r15/StateMachine...
Celeste controller:
https://github.com/NoelFB/Celeste/blo...
Fantastic article about State Machine:
https://www.raywenderlich.com/6034380...

00:00 Question?
00:18 What's the problem?
01:01 Regular Player Controller
02:00 General Idea & Benefits
05:47 Code explanation - intro
06:15 Code explanation - state
10:00 Creating states as Scritable Objects
10:37 Code explanation - example state (walk)
13:23 Code explanation - example state (jump)
15:12 Code explanation - state runner
18:40 CHALLANGE QUESTION
19:58 Code explanation - character controller


Watch video How to implement Finite State Machine in Unity online without registration, duration hours minute second in high quality. This video was added by user PitiIT 27 April 2022, don't forget to share it with your friends and acquaintances, it has been viewed on our site 15,736 once and liked it 385 people.