Skip to content

Enemy AI

Enemy AI is a state machine made of ScriptableObjects. A behavior asset (EnemyBehaviourSO) combines states and a prioritized action list; assign it to the enemy's EnemyBrain. The attack kit stays on the enemy, so one behavior can serve many enemies.

How a turn works

In the Combat state, actions are tried in order:

  • NotApplicable: try the next action;
  • Done: run the entry's follow-ups (each rolls its chancePercent) and end the turn;
  • Held: end the turn without follow-ups.

States in Data/Enemy/AI/States: State_Idle (switches to Combat the turn the player enters aggro), State_Combat, State_Charge (charges N turns, fires in the locked direction).

Actions in Data/Enemy/AI/Actions: Action_Summon, Action_Retreat (closer than preferredMinRange), Action_Attack (first valid kit option), Action_Strafe, Action_SeekFiringPosition, Action_Surround (melee spreads out instead of queuing), Action_StepTowardsPlayer (one A* step), Action_FollowUpAttack_50/60/70 (uncharged, % damage).

Included behaviors

Asset (Data/Enemy/AI/Behaviours) Range Actions (follow-ups) Used by
Behaviour_Basic – Attack, StepTowards SkeletonArquebusier, MageBossMinion
Behaviour_Melee 1–1 Attack, StepTowards (70% dmg, 30%) Spider, OrcBrute
Behaviour_Boss – Summon, Attack, StepTowards MageBoss
Behaviour_Brute 1–1 Attack, Surround and StepTowards (70%, 35%) OrcBrute_Brute
Behaviour_Gunner 2–6 Attack (Strafe 30%), SeekFiring, Surround, StepTowards (60%, 25%) ..._Gunner
Behaviour_Heavy 2–7 Attack, SeekFiring, StepTowards ..._Heavy
Behaviour_Skirmisher 3–6 Retreat, Attack (Strafe 50%), SeekFiring, Surround, StepTowards ..._Skirmisher

So Spider and OrcBrute attack when they can; otherwise they step toward the player, with a 30% chance of a follow-up attack in the same turn.

Create a behavior without code

Duplicate a behavior asset (or Create > AiasTech > Enemy > AI), set preferredMinRange/preferredMaxRange, order actions by priority, add follow-ups with chances, and assign it to EnemyBrain.behaviour.

Write an action (C#)

using AiasTech.Enemy;
using AiasTech.Enemy.Actions;
using UnityEngine;

[CreateAssetMenu(fileName = "Action_HealAlly", menuName = "AiasTech/Enemy/AI/Actions/Heal Ally")]
public class HealAllyActionSO : EnemyActionSO
{
    [SerializeField] private float amount = 10f;

    public override EnemyActionResult Execute(EnemyContext context)
    {
        foreach (EnemyBrain other in EnemyBrain.ActiveBrains)
        {
            if (other.Context == context || other.Context.IsDead) continue;
            if (other.Context.Entity.CurrentHealth >= other.Context.Entity.MaxHealth) continue;
            other.Context.Entity.RegenerateHealth(amount);
            return EnemyActionResult.Done;
        }
        return EnemyActionResult.NotApplicable;
    }
}

Create the asset from the menu and add it to a behavior's actions.

Write a state (C#)

Inherit EnemyStateSO and implement IEnumerator RunTurn(EnemyContext context). Call context.BeginActiveTurn() when the turn counts for regeneration and cooldowns, and yield return context.WaitUntilIdle() before decisions that depend on a finished move. Add it to extraStates and enter it with context.RequestState(context.Behaviour.FindState<YourStateSO>()).

Assets are shared

Never store per-enemy data in state or action fields. Use context.Blackboard with typed BlackboardKey<T> keys.

For the full EnemyContext API, see Assets/DungeonCrawler/Scripts/Enemy/ARCHITECTURE.md.

Debug and limits

Select an enemy to see its aggro range and, in Play mode, its current state. The target is always the player; there is no built-in vision cone, hearing, memory or patrol.