Skip to content

Unity-King-Technologies/Unity-Game-Enemy-ai-Framework-Unityking

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

7 Commits
Β 
Β 
Β 
Β 

Repository files navigation

Unity Game Enemy AI Framework

FSM β€’ Behavior Trees β€’ Perception System

Created and maintained by Unity King
https://unityking.com


Quick Start (Read This First)

This repository contains only the Assets/ folder with a complete, modular Enemy AI framework.

How to Use This Repository

  1. Create or open a Unity project

    • Unity 2021 LTS or newer recommended
  2. Copy the Assets folder from this repository
    Paste it directly into your Unity project root
    (merge with existing Assets if needed)

  3. Open Unity

    • Unity will automatically import all scripts
  4. Create an Enemy GameObject

    • Add required components (explained below)

That’s it. The framework is now ready to use.


πŸ“ Project Structure

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.


Architecture Overview

This framework combines three AI techniques:

Finite State Machine (FSM)

Used for high-level behavior:

  • Idle
  • Patrol
  • Chase
  • Attack

FSM decides what the enemy should do.


Behavior Trees (BT)

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)

Perception System

Used to sense the environment:

  • Vision-based detection
  • Player awareness
  • Target tracking

Core Components Explained

EnemyAIController

The brain of the enemy.

Responsibilities:

  • Initializes the FSM
  • Updates current state
  • Shares data via Blackboard
  • Connects Perception and Movement
EnemyAIController

Attach this to every enemy GameObject.


EnemyBlackboard

Shared memory between:

  • FSM
  • Behavior Trees
  • Sensors

Stores:

  • Current target
  • Distance to target
  • Visibility
  • Attack range status
blackboard.target
blackboard.isInAttackRange

Finite State Machine (FSM)

Available States

  • IdleState
  • PatrolState
  • ChaseState
  • AttackState

Each state:

  • Implements IState
  • Has Enter(), Tick(), Exit()

Example: Idle β†’ Chase Transition

public void Tick()
{
    if (ai.blackboard.target != null)
        ai.ChangeState(new ChaseState(ai));
}

FSM controls macro behavior flow.


Behavior Trees

Behavior Trees are used for fine-grained logic.

Core Nodes

  • Selector β†’ Try children until one succeeds
  • Sequence β†’ Execute all children in order
  • ActionNode β†’ Actual gameplay logic

Example: ActionNode

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.


Perception System

VisionSensor

Detects targets using physics overlap.

Features:

  • Distance-based detection
  • Layer filtering
  • Real-time updates
Collider[] hits = Physics.OverlapSphere(
    transform.position,
    viewDistance,
    targetLayer
);

EnemyPerception

Acts as a bridge between sensors and AI logic.

CurrentTarget = vision.DetectedTarget;

Enemy Movement

EnemyMovement

Handles physical movement.

Currently supports:

  • Direct movement toward target
movement.MoveTo(target.position);

Can be extended to:

  • NavMeshAgent
  • Root motion
  • Flying / swimming AI

Example Enemy Setup (Step-by-Step)

  1. Create an empty GameObject Enemy

  2. Add components:

    • EnemyAIController
    • EnemyPerception
    • VisionSensor
    • EnemyMovement
    • (Optional) Collider / Rigidbody
  3. Set VisionSensor:

    • View Distance
    • Target Layer (Player)
  4. Press β–Ά Play

Enemy will:

  • Stay idle
  • Detect player
  • Chase
  • Attack (logic ready)

Extending the Framework

You can easily add:

FSM

  • FleeState
  • SearchState
  • AlertState

Behavior Tree

  • Cooldown decorators
  • Inverter nodes
  • Repeater nodes

Perception

  • Hearing sensor
  • Damage sensor
  • Line-of-sight checks

Movement

  • NavMesh pathfinding
  • Strafing
  • Cover system

Use Cases

  • FPS / TPS enemies
  • Stealth games
  • Survival AI
  • Boss behavior systems
  • R&D AI experiments

License

MIT License Free for personal and commercial use.


Author

Unity King https://unityking.com Game Development β€’ AI Systems β€’ Tools


⭐ If this framework helps you, consider starring the repository.

About

Enemy AI jo smart feel kare FSM Behavior Trees Perception system

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages