Enemy AI State Creator Documentation

Blog post description.

4/10/20252 min read

worm's-eye view photography of concrete building
worm's-eye view photography of concrete building

Enemy AI State Creator Documentation

The EnemyAIStateCreator is a custom Unity Editor tool designed to streamline the creation of new enemy AI states for the EnemyAIStateMachine system in the SEBURO TPS project. This tool generates a new EnemyAIState ScriptableObject script with a basic template, allowing you to quickly define custom behaviors for enemy AI.

Overview

  • Purpose: Automates the creation of enemy state scripts to reduce manual boilerplate coding.

  • Location: Assets\SEBURO TPS\Scripts\STPS Enemy\Editor\EnemyAIStateCreator.cs

  • Menu Path: Window > STPS > EnemyAI > Create New Enemy State

  • Output: Generates a .cs file in your specified directory (defaults to Assets\SEBURO TPS\Scripts\STPS Enemy/States).

Prerequisites

  • Unity Version: Compatible with Unity 2019.4 or later (tested as of April 2025).

  • Required Scripts:

    • EnemyAIStateMachine.cs (with EnemyAIState base class) in Assets\SEBURO TPS\Scripts\STPS Enemy.

    • STPSEnemyMovement.cs in Assets\SEBURO TPS\Scripts\STPS Enemy.

  • Folder Structure: Ensure an Editor folder exists under Assets\SEBURO TPS\Scripts\STPS Enemy for the creator script, and optionally a States subfolder for generated states.

How to Use

Step 1: Access the Tool

  1. Open Unity Editor.

  2. Navigate to the top menu: Window > STPS > EnemyAI > Create New Enemy State.

  3. The "Create New Enemy AI State" dialog will appear.

Step 2: Create a New State

  1. File Name: Enter a name for your new state (e.g., "PatrolState", "ChaseState").

    • If the name doesn’t end with "State", it will be appended automatically (e.g., "Patrol" becomes "PatrolState").

  2. Save Location: Choose where to save the script.

    • Default directory: Assets\SEBURO TPS\Scripts\STPS Enemy/States.

    • You can browse to another folder if preferred.

  3. Confirm: Click "Save" to generate the script.

    • If you click "Cancel" or leave the name blank, the process aborts.

Step 3: Verify the Output

  • Result: A new .cs file is created at the specified path (e.g., PatrolState.cs).

  • Project Window: The script is automatically selected and pinged in the Project window for easy access.

  • Console: A log message confirms creation (e.g., "Created new enemy state script: Assets/.../PatrolState.cs").

Step 4: Customize the State

  1. Open the generated script in your code editor.

  2. Modify the template to define your state’s behavior (see "Generated Template" below).

  3. Save the script, and Unity will compile it.

Step 5: Integrate with EnemyAIStateMachine

  1. In the Unity Editor, create an instance of the new state:

    • Right-click in Project window > Create > Enemy AI > States > [YourStateName].

  2. Assign the instance to an enemy’s EnemyAIStateMachine:

    • Select an enemy GameObject with EnemyAIStateMachine.

    • Drag the state asset into the States list in the Inspector.

    • Optionally set it as the Initial State if it should start active.

Generated Template

The tool generates a script based on this template:

  • [StateName]: Replaced with your provided name (e.g., "PatrolState").

  • Comments: The SetState line is an example and commented out, as not all state names directly match STPSEnemyMovement.EnemyState enum values.

Customization Tips

  • Add Fields: Include serialized fields like [SerializeField] private float speed = 1.5f; for state-specific settings.

  • Set State: Uncomment and adjust _movement.SetState to match an EnemyState enum (e.g., Patrol, Chase), or remove it if not applicable.

  • Transitions: Modify CheckTransitions to define when to switch states based on StateMachine.Perception, Combat, etc.

  • Movement: Use _movement methods like SetTarget, SetWalkSpeed, or Investigate to control enemy behavior.

Example: Creating PatrolState

  1. Go to Window > STPS > EnemyAI > Create New Enemy State.

  2. Name it "PatrolState" and save to Assets\SEBURO TPS\Scripts\STPS Enemy/States.

  3. Open PatrolState.cs and customize: