Created and maintained by Unity King
https://unityking.com
This repository contains only the Assets/ folder with a complete, modular Enemy AI framework.
-
Create or open a Unity project
- Unity 2021 LTS or newer recommended
-
Copy the
Assetsfolder from this repository
Paste it directly into your Unity project root
(merge with existing Assets if needed) -
Open Unity
- Unity will automatically import all scripts
-
Create an Enemy GameObject
- Add required components (explained below)
Thatβs it. The framework is now ready to use.
Assets/
βββ Scripts/
β βββ EnemyAI/
β β βββ Core/
β β β βββ EnemyAIController.cs
β β β βββ EnemyBlackboard.cs
β β βββ FSM/
β β β βββ IState.cs
β β β βββ StateMachine.cs
β β β βββ IdleState.cs
β β β βββ PatrolState.cs
β β β βββ ChaseState.cs
β β β βββ AttackState.cs
β β βββ BehaviorTree/
β β β βββ BTNode.cs
β β β βββ Selector.cs
β β β βββ Sequence.cs
β β β βββ ActionNode.cs
β β βββ Perception/
β β β βββ EnemyPerception.cs
β β β βββ VisionSensor.cs
β β βββ Movement/
β β βββ EnemyMovement.cs
Each folder represents a single responsibility system, making the framework easy to understand, debug, and extend.
This framework combines three AI techniques:
Used for high-level behavior:
- Idle
- Patrol
- Chase
- Attack
FSM decides what the enemy should do.
Used for decision-making logic inside states.
BT decides how the enemy should act:
- Selector (OR logic)
- Sequence (AND logic)
- Action Nodes (actual gameplay logic)
Used to sense the environment:
- Vision-based detection
- Player awareness
- Target tracking
The brain of the enemy.
Responsibilities:
- Initializes the FSM
- Updates current state
- Shares data via Blackboard
- Connects Perception and Movement
EnemyAIControllerAttach this to every enemy GameObject.
Shared memory between:
- FSM
- Behavior Trees
- Sensors
Stores:
- Current target
- Distance to target
- Visibility
- Attack range status
blackboard.target
blackboard.isInAttackRangeIdleStatePatrolStateChaseStateAttackState
Each state:
- Implements
IState - Has
Enter(),Tick(),Exit()
public void Tick()
{
if (ai.blackboard.target != null)
ai.ChangeState(new ChaseState(ai));
}FSM controls macro behavior flow.
Behavior Trees are used for fine-grained logic.
Selectorβ Try children until one succeedsSequenceβ Execute all children in orderActionNodeβ Actual gameplay logic
ActionNode chaseTarget = new ActionNode(() =>
{
if (blackboard.target == null)
return BTNode.State.Failure;
movement.MoveTo(blackboard.target.position);
return BTNode.State.Running;
});This makes Behavior Trees code-driven and flexible.
Detects targets using physics overlap.
Features:
- Distance-based detection
- Layer filtering
- Real-time updates
Collider[] hits = Physics.OverlapSphere(
transform.position,
viewDistance,
targetLayer
);Acts as a bridge between sensors and AI logic.
CurrentTarget = vision.DetectedTarget;Handles physical movement.
Currently supports:
- Direct movement toward target
movement.MoveTo(target.position);- NavMeshAgent
- Root motion
- Flying / swimming AI
-
Create an empty GameObject
Enemy -
Add components:
EnemyAIControllerEnemyPerceptionVisionSensorEnemyMovement- (Optional) Collider / Rigidbody
-
Set VisionSensor:
- View Distance
- Target Layer (Player)
-
Press βΆ Play
Enemy will:
- Stay idle
- Detect player
- Chase
- Attack (logic ready)
You can easily add:
- FleeState
- SearchState
- AlertState
- Cooldown decorators
- Inverter nodes
- Repeater nodes
- Hearing sensor
- Damage sensor
- Line-of-sight checks
- NavMesh pathfinding
- Strafing
- Cover system
- FPS / TPS enemies
- Stealth games
- Survival AI
- Boss behavior systems
- R&D AI experiments
MIT License Free for personal and commercial use.
Unity King https://unityking.com Game Development β’ AI Systems β’ Tools
β If this framework helps you, consider starring the repository.