Getting Started

Get up and running with State Machine 2 in minutes

This guide will help you create your first state machine in just a few minutes.

Prerequisites

Before you begin, ensure you have:

Step 1: Create a State Machine Asset

  1. In the Project window, navigate to where you want to create your state machine

  2. Right-click and select Create โ†’ Ninjutsu Games โ†’ State Machine

  3. Name your state machine (e.g., "EnemyAI" or "PlayerController")

Step 2: Open the Graph Editor

Double-click your new State Machine asset to open the Graph Editor.

You'll see a canvas with a Start Node and Exit Node already created. These nodes are automatically added to every new state machine and cannot be deleted.

Step 3: Add an Actions Node

Let's add some behavior:

  1. Right-click on the canvas

  2. Select Create Node โ†’ Actions

  3. Click on the new Actions node to select it

  4. In the Inspector, click Add Action

  5. Choose an action (e.g., Debug โ†’ Log Message)

  6. Configure the action (e.g., type "Hello from State Machine!")

Step 4: Connect the Nodes

  1. Click on the output port (right side) of the Start node

  2. Drag the connection to the input port (left side) of the Actions node

  3. Release to create the connection

Step 5: Add a State Machine Runner

The Runner component executes your state machine on a GameObject:

  1. Create or select a GameObject in your scene

  2. Click Add Component

  3. Search for State Machine Runner

  4. Drag your State Machine asset to the State Machine field

You can also create a runner via Create โ†’ Ninjutsu Games โ†’ State Machine Runner

Step 6: Test It!

  1. Press Play in Unity

  2. Check the Console window โ€” you should see your log message!

๐ŸŽ‰ Congratulations! You've created your first state machine!


Next Steps

Now that you have the basics, explore these topics:

Add More Behavior

Use Variables

  • Variables โ€” Store and share data between nodes

Learn the Editor

Build Real Systems

Try building these common use cases:

System
Nodes to Use

Enemy AI

Start โ†’ Trigger (On Player Near) โ†’ Actions (Chase)

Door Controller

Trigger (On Interact) โ†’ Branch (Is Locked?) โ†’ Actions

Ability Cooldown

Actions โ†’ Trigger (On Timer) โ†’ Actions (Ready)

Game State Manager

Start โ†’ Sub-State Machine (Menu, Playing, Paused)


Example: Simple Enemy AI

Here's a complete example of a basic enemy AI:

Nodes Setup

  1. Start Node โ€” Entry point

  2. Trigger Node โ€” "On Player Enter" (using a trigger collider)

  3. Actions Node โ€” "Chase Player" (move towards player)

  4. Trigger Node โ€” "On Player Exit"

  5. Actions Node โ€” "Return to Patrol"

Connections

This creates a loop where the enemy:

  1. Starts idle

  2. Detects the player โ†’ starts chasing

  3. Loses the player โ†’ returns to idle


Troubleshooting

State Machine Not Running

  • Ensure the State Machine Runner component is on an active GameObject

  • Check that the State Machine field has an asset assigned

  • Verify the Start node has at least one output connection

Actions Not Executing

  • Check if nodes are enabled (not grayed out)

  • Verify connections exist between nodes

  • Use Live Debug in the Graph Editor to see execution flow

Need Help?

Last updated

Was this helpful?