Unity
C#
WebGL
State Machine AI
NavMesh
Gameplay Systems

AI NPC Behavior System

Adaptive NPC behavior built in Unity using AI-driven gameplay logic, state machines, and real-time environmental analysis.

Load Interactive Demo

Click to initialize the WebGL engine (~15MB). Best experienced on desktop.

Pro Tip: Use the fullscreen button in the bottom right for the best experience. Click inside the game to lock controls.

Overview

This lab explores adaptive NPC behavior systems where AI reacts dynamically to player actions and aggression levels. Instead of relying on simple scripted sequences, the NPC utilizes a robust finite state machine coupled with spatial awareness (via Unity's NavMesh) to evaluate threats, decide whether to engage or flee, and investigate disturbances. The goal is to demonstrate a scalable architecture for enemy AI that feels organic and responsive to player input.

Animated Unity WebGL demo showing AI NPC patrol, chase, and attack behavior
NPC transitions between patrol, chase, and attack states based on player behavior.

Features & Capabilities

Patrol AI

Dynamic waypoint generation and randomized patrol paths across the NavMesh.

Chase Logic

Line-of-sight detection triggers high-speed pursuit and interception calculations.

Attack State

Proximity-based combat engagement with randomized strike timing.

Retreat System

Calculates escape vectors when health is low or player aggression spikes.

Search Behavior

Investigates last known positions when visual contact is lost.

Aggression Tracking

Monitors player DPS to dynamically scale NPC defensive responses.

Technical Architecture

The core logic is driven by a deterministic 5-state Finite State Machine (NPCBrain) coupled with vision-cone and proximity perception (CanSensePlayer), NavMeshAgent steering, and rolling player aggression tracking (PlayerBehaviorTracker).

Full Engineering Case Study & C# Breakdown

Inspect the complete state transition tables, C# perception and NavMesh code, and WebGL performance analysis.

Challenges & Learnings

One of the primary challenges in this prototype was balancing the 'Search' behavior to prevent the NPC from looking robotic when it lost the player. Implementing a localized grid-search algorithm combined with randomized wait times created a much more convincing illusion of intelligence. Additionally, adapting the WebGL build to ensure smooth input handling and screen scaling required custom configuration in the Unity WebGL template.

Want to build AI gameplay systems?

I specialize in engineering production-ready Unity systems and intelligent NPC architectures for scalable games.

Need to deploy directly to browsers? Explore my optimized WebGL Game Development solutions.