0
0
Unityframework~5 mins

State machines for AI behavior in Unity

Choose your learning style9 modes available
Introduction

State machines help AI decide what to do next by organizing actions into clear steps. They make AI behavior easy to understand and control.

When you want a game character to switch between walking, running, and attacking.
When an enemy needs to patrol, chase the player, or hide.
When you want to control a robot's tasks like searching, picking up items, and returning.
When you want to make sure AI only does one action at a time and changes smoothly.
When you want to avoid confusing or random AI behavior.
Syntax
Unity
public enum State {
    Idle,
    Patrol,
    Chase,
    Attack
}

public State currentState;

void Update() {
    switch (currentState) {
        case State.Idle:
            // do idle stuff
            break;
        case State.Patrol:
            // do patrol stuff
            break;
        case State.Chase:
            // do chase stuff
            break;
        case State.Attack:
            // do attack stuff
            break;
    }
}

Use enums to list all possible states clearly.

Use a switch or if-else to decide what to do based on the current state.

Examples
This example shows how to define states and set the starting state.
Unity
public enum State { Idle, Walk, Run }

public State currentState = State.Idle;
Using if-else to handle different states in the update loop.
Unity
void Update() {
    if (currentState == State.Idle) {
        // wait
    } else if (currentState == State.Walk) {
        // move slowly
    } else if (currentState == State.Run) {
        // move fast
    }
}
Using switch-case to print messages based on the current state.
Unity
switch (currentState) {
    case State.Idle:
        Debug.Log("Standing still");
        break;
    case State.Walk:
        Debug.Log("Walking around");
        break;
    case State.Run:
        Debug.Log("Running fast");
        break;
}
Sample Program

This program shows a simple AI that starts idle. If the player gets close, it switches to chase. If the player moves away, it goes back to idle.

Unity
using UnityEngine;

public class SimpleAI : MonoBehaviour {
    public enum State {
        Idle,
        Patrol,
        Chase
    }

    public State currentState = State.Idle;
    public Transform player;
    public float chaseDistance = 5f;

    void Update() {
        switch (currentState) {
            case State.Idle:
                Debug.Log("AI is idle.");
                if (Vector3.Distance(transform.position, player.position) < chaseDistance) {
                    currentState = State.Chase;
                }
                break;

            case State.Patrol:
                Debug.Log("AI is patrolling.");
                // Patrol logic here
                break;

            case State.Chase:
                Debug.Log("AI is chasing the player!");
                // Chase logic here
                if (Vector3.Distance(transform.position, player.position) > chaseDistance) {
                    currentState = State.Idle;
                }
                break;
        }
    }
}
OutputSuccess
Important Notes

Keep states simple and clear to avoid confusion.

Use distances or events to switch between states smoothly.

Debug.Log helps you see what the AI is doing while testing.

Summary

State machines organize AI actions into clear steps.

Use enums and switch or if-else to control AI behavior.

Switch states based on conditions like player distance or timers.