
Discover state machines as a powerful, organized technique for modeling character and object behavior in games, with states, transitions, and avoiding spaghetti code in Unity.
Explore coding conventions, preferences, and habits for Unity C# development, featuring vim-driven editing, underscore private variables, properties, namespaces, and modular state-machine oriented design.
Download and install Unity via Unity Hub and create a universal render pipeline project. Explore scene, hierarchy, view, assets, and package manager; configure input system and git in VS Code.
Import the 2D sprite package and golden chest animations from the pixel art platformer, organize assets in a third party folder, and prep the chest textures in the sprite editor.
Initialize a Git repository for your Unity project, create a Unity git ignore, and use a client like Fork to commit checkpoints and roll back changes.
Create a chest scene in Unity by organizing assets, setting an orthographic 2D camera, and building a chest open and close state machine with an animator and triggers.
Learn to split chest logic from the view in Unity by designing a chest view and a chest controller that communicate via an onclick event, enabling clean, maintainable code.
Toggle the chest state with a boolean isClosed on click, update the view, and trigger the chest open animation with the animator; this shows a simple non state machine approach.
Learn basic state machine concepts by modeling a chest with a boolean for open or closed. Expand to an enum with multiple states and transitions.
import and set up a stylized bear from the asset store in Unity, configure materials and textures, then add ProBuilder and Grid Box Prototype for testing bear states.
Set up a bear scene in Unity by importing the bear prefab, centering it with a zero transform, adding a base cylinder, and adjusting the camera to solid color background.
Create a new bear animator in Unity, import bear idle and walk animations, and set up an empty state machine to drive idle, walk forward, and walk back motions.
Enable the old input system and read horizontal and vertical axes with Input.GetAxis to form a Vector2 input, then feed it to the animator during update.
Drive unity animations by crossfading between idle, walk forward, and walk back based on w input, while illustrating how a clunky spaghetti approach motivates a state machine.
Add an eat animation to the bear by pressing E, crossfading via the animator, and expose the messy, hard-to-scale state logic that motivates exploring state machines for cleaner design.
Implement a floor moving effect in Unity by using the bear’s y input to scroll the floor texture offset via the material, renderer, and on move event, prepping state machines.
Learn to build a 2d character driven by a state machine, create an assets-based Unity URP scene, import sprites, slice frames, and craft an idle animation with the character animator.
Split the player into a 2D rigidbody with a capsule collider and an animator child to handle visuals. Add a box collider 2D for the floor and constrain z rotation.
Create a root namespace named state machines to wrap all scripts, organizing code into logical boxes. Use the state machines namespace to access bear, floor, and chest controllers and views.
Design a modular character controller with an action reader that reads input actions, converts them into a normalized move vector, and delegates movement and input handling to dedicated components.
Create a character movement component for a Rigidbody2D to control velocity via set velocity from move action inputs, and flip the character by updating the local scale.
Slice a walk sprite sheet into eight frames, create walk and idle animations in the animator, and wire input to switch between them while laying groundwork for a state machine.
Explore building a reusable Unity state machine with an IState interface, enter, execute, and exit methods, a dictionary of states for runtime updates, including idle and walk transitions.
Implement a serializable state machine in Unity, add states by mapping names to implementations, and expose a public owner property so states can trigger transitions.
Implement the state machine update by grabbing the current state and calling its execute method, enabling runtime swapping between idle, walk, and other states from the character controller's update.
Change states in a Unity state machine by exiting the current state and entering a new one via a dictionary lookup. Check state names and log invalid ones.
Define and expose the current state name of a Unity state machine by adding a serialized string field, updating it when states change, and viewing it in the inspector.
Instantiate a private serialized state machine in the chest controller, initialize it in the awake method, and define the closed state as the default with behavior inside the state.
Learn to attach a state machine to a chest, define closed and open states with an enum, create a chest state class, and implement transitions and animations using code.
Create the chest open state with a new script and constructor referencing the chest controller. Toggle open and close with space, triggering animations via the state machine.
Demonstrates handling the chest on click with a Unity state machine: subscribe to the view's on click on enter, unsubscribe on exit, and toggle between closed and open states.
Create a utility to generate boilerplate state scripts for Unity state machines, reducing repetitive code by using a template and a custom create state script utility.
Implement a bear state machine in Unity, adding idle, walk forward, walk back, and eat states, wiring transitions, and animator crossfades to drive animations via input.
Convert the character into a state machine by adding an idle state that plays the idle animation and sets velocity to zero, paving the way for a walk state.
Define and integrate a walk state into the state machine, mirroring idle logic but applying input-driven velocity and a walk animation with transitions based on move action input.
Split data from logic by using a scriptable object for character properties, like walk speed, and connect it to movement to drive the state machine and animation.
Import the jump sprite, slice the image, create a jump animation from two sprites, and apply it in the animator to implement the jump state.
Add a jump state to the Unity character state machine, reading the jump action, playing the jump animation, and applying vertical velocity with transitions from idle and walk when pressed.
Prepare the fall animation using two sprites in the scene and configure the animator to transition from jump to fall as a sequence. Clean up frames and finalize the state.
Detect grounded state in Unity 2D by casting a downward ray. Use an obstacle layer, a configurable ray length, and gizmos for debugging in the state machine.
Implement a fall state in a Unity state machine by using velocity and grounding to transition from jump or idle to fall when velocity.y is negative, and play fall animation.
Create and integrate a crouch state in a Unity state machine, including crouch idle and crouch walking, by importing sprites, slicing frames, and building the crouch animation in the animator.
Create a crouch state in Unity using an animator substate, with transitions from idle and walk triggered by move action y < 0, and play the crouch idle animation.
Explore crouch walk within a crouch state using a hierarchical state machine, with a crouch walk animation in a sub state and velocity-based movement driven by crouch speed.
Create and integrate a raw animation by converting sprites, setting 16-pixel sprites with point compression, and building a row animation inside the crouch substate of the character.
Build a crouch to roll state in a unity state machine by binding left ctrl to roll, tracking move input, applying roll speed, and returning to crouch when animation ends.
Implement a crouch state by dynamically resizing and offsetting the capsule collider 2d during crouch, then restoring it when standing, and connect these changes to movement and velocity.
Implement a crouch sensor in Unity using a head space collider trigger to detect obstacles. Update a sensor model and feed the state machine to determine when to uncrouch.
Import run sprites, set 16 pixels per unit, slice into eight frames, create a run animation, and attach it to the player animator beside the walk, removing the run animator.
Implement the run state in the Unity state machine, play the run animation on enter, move with run speed when shift is held, and transition from walk to run.
Create a push hands object as a child of the player with a hands collider to push boxes using Unity physics, tuning a 2D box collider's size and offset.
Create a pushable box in a 2D Unity scene by implementing the IPushable interface on a MonoBehaviour, using Rigidbody2D and BoxCollider2D, with mass toggling between 50 and 2 during push.
Improve sensors to detect a pushable box using a raycast array, facing direction, and obstacle layer masking, enabling push/pull interactions in Unity 2D.
Improve push-pull interactions by wiring the interaction button to the action reader, detecting a pushable with sensors and raycast, and transitioning from idle to push-pull idle to enable related animations.
Create a push/pull idle animation in Unity by slicing the character sprite sheet into eight frames, dragging them to form the animation, and adjusting the pivot to 0.50–0.54.
Implement a push/pull idle state in Unity using a sub state machine, configure animations and transitions, and define exit conditions based on pushable range, movement direction, and the interaction key.
Create and integrate a push animation in the Unity state machine by preparing the sprite (sprite mode 16, custom pivot, slice) and attaching it to the character animator.
Learn to implement a push state in Unity using a hand collider, enable/disable logic, and precise transitions between idle and push, including push speed and animation control.
Fix push/pull warp position in the Unity state machine by computing the warp point from the collision hit and facing direction, then teleport the owner to that consistent position.
Learn to implement pullable boxes in Unity by adding a pull sensor and pull point, reusing pushable logic, and applying layer filters to detect pullables and enter the pull state.
Create an I pullable interface and a pullable point as monobehaviours, using a fixed joint 2D to connect the player's rigidbody when pulling, and adjust mass for push/pull states.
Create a pullable box by duplicating a pushable box, add pullable point and handle, attach a rigid body with a fixed joint, and configure collider on obstacle layer for raycasting.
Learn to implement a pull sensor in Unity by using a box collider trigger, sensor layer management, and trigger enter/exit logic to detect pullable objects and broadcast changes.
Connect the pull sensor to the character by wiring a serialized sensor reference, then listen for pull events and update the character sensor data with the pullable.
Set up a pull state in Unity by converting the pull animation to a sliced sprite, placing it in the scene, and adjusting the animation sample for a smooth pull.
Create the pool state in the Unity state machine, initialize the state key, play the pull animation, and handle entering direction and transitions to pull or idle based on pullables.
Implement a pull state in Unity by connecting a pullable object to the ball with a joint, enabling velocity-based movement, and ignore flip during push/pull and idle transitions.
Implement ladder climbing in Unity by entering a climb state with the W key and stepping up or down via four animation events, wiring public methods to those events.
Define the IClimbable interface with bottom and top point methods, implement a climb sensor using OnTriggerEnter2D and OnTriggerExit2D, and detect climbable objects to drive ladder interactions.
Fix ladder collisions by ignoring the default layer and using the obstacle layer, add a sorting group, and implement a ladder script with bottom and top points and gizmos.
Build a climbable sensor in Unity by linking the climb sensor to the character sensor, updating sensor data for climbable state, and validating trigger-based detection during play.
Learn to bind and sync Unity animations with scripts using animation events, create a character animation events script with public methods and events, and wire end-of-animation listeners for sequencing.
Define and implement a climb state in a Unity state machine, transitioning from idle when a climbable is detected and the up arrow is pressed, and play the climb animation.
During the climb state, use animation events to trigger upward movement by 0.5 units, reading input and moving the character, while temporarily disabling rigidbody simulation.
Implement a climb state in Unity that rests when no key is pressed. Pause the animator by setting speed to zero and map up and down inputs to their animations.
Demonstrates a climb state that warps the player to the closest ladder point (bottom or top) by using the climbable interface and distance comparisons.
Enhances a state machine to handle climbing a ladder, including entering from bottom, exiting at top, using up/down input with distance checks, a top offset, and transitioning to idle.
Master state machines in games by exploring jump, walk, and run behaviors, sensors, and artificial intelligence in Unity, using rigidbodies, colliders, and scalable design.
Say goodbye to spaghetti code, no more floating variables intercommunicating for reasons that you cant remember the day after. State Machines is a powerful technique that will allow you to avoid spaghetti code, make it scalable and easier to maintain.
Learn techniques that professional Game Developers from Industry are vastly using to create complex games.
This course is designed for game programmers that want to create really scalable code using techniques that are vastly used in many AAA games that you have played.
Not only learning how to implement State Machine in any of your games but during this course you will learn a lot of cool tips, techniques and ideas on how to architect your code, how to write code that is easier to maintain and scale, useful insights and a little bit of philosophy that will make you a more mature developer.
Introduction - Here I will explain more about the idea of state machines, present a few of my coding conventions and habits that can be useful for you and will install and have an overview in the Unity Editor.
Chest Example - In this section I will create the most basic example that we can (later) apply state machines. Here we still don't know about state machines but this is not an excuse to discuss and write GOOD code.
Bear Example - Here you will learn what a bad code is. We will implement the behavior of the Bear using spaghetti code to the point it is unproductive and you will be asking "okay, I got it but what can we do about it?"
Character Example - At this point we are preparing the ground to dive into State Machines but first we need some base thing to control and some code architecture to use
State Machine Implementation - Create a simple and reusable code to use in this project and in any other Unity C# project that you want.
Refactoring with State Machines - Lets reopen the Chest example and now refactor it using our new knowledge of State Machines.
Character Example (Part 2) - This is the core of the course, here is where you will definitely learn state machines applying over and over again until you are tired and the concept is in your head.
Final Challenge - Now that you are used to State Machines we are going to develop a more complex case using it so you can cover more different details to be able to cover the need for your games.
By the end of this course you will be tired of hearing the word "State Machine" but happy to have this new ability to implement anything in your mind.