TExcelTAD
Home › Game AI › Game AI Intro

Game AI Intro

Game AI is the code that makes non-player characters look like they have a mind of their own: the guard who hears a noise and goes to look, the wolf pack that circles you, the racing opponent who takes the inside line, the strategy rival who attacks your weakest town. It is a different discipline from machine learning. A game AI doesn't need to be intelligent. It needs to look intelligent, stay predictable enough to learn, run in a fraction of a millisecond, and above all make the game fun.

What this course covers

Section Question it answers Main techniques
Decisions What should I do now? finite state machines, hierarchical FSMs, behavior trees, blackboards, utility AI, GOAP
Movement How do I move there smoothly? seek, flee, arrive, pursuit, wander, avoidance, flocking
Pathfinding Which way gets me there? graphs and grids, BFS, Dijkstra, A*, heuristics, smoothing, navmeshes, flow fields
Perception What do I know about the world? vision cones, hearing, memory and suspicion
Tactics Where should we stand, and who does what? cover points, influence maps, squads
Engine mapping How do Unity, Unreal and Godot do it? NavMeshAgent, Unity Behavior, AIController, Behavior Trees, EQS, StateTree, AI Perception, NavigationAgent3D

Every algorithm is written in plain C# that you can run right here, printing positions, paths and decisions step by step. Movement, pathfinding and vision also have live canvas demos you can play with. The engine lessons show the real engine APIs next to these models, so you can see what the engine is doing for you.

The smallest useful AI

Here is a whole enemy in a dozen lines: it patrols until the player comes within 5 units, then chases, and attacks when within 1.5 units. The "world" is one dimension and the player walks past at a fixed speed, so the output is identical every time you run it.

csharp · try it yourself
using System;

double enemy = 0, player = 12, patrolDir = 1;
string state = "Patrol";

for (int tick = 0; tick < 12; tick++)
{
    double dist = Math.Abs(player - enemy);

    // Think: pick a state from what the enemy can sense.
    state = dist <= 1.5 ? "Attack" : dist <= 5 ? "Chase" : "Patrol";

    // Act: each state moves differently.
    if (state == "Patrol")
    {
        enemy += 0.5 * patrolDir;
        if (enemy > 3 || enemy < 0) patrolDir = -patrolDir;
    }
    else if (state == "Chase")
    {
        enemy += Math.Sign(player - enemy) * 1.0;
    }

    Console.WriteLine(
quot;tick {tick,2}: enemy {enemy,4:F1} player {player,4:F1} dist {dist,4:F1} {state}"); player -= 0.6; // the player walks towards the enemy }
tick  0: enemy  0.5  player 12.0  dist 12.0  Patrol
tick  1: enemy  1.0  player 11.4  dist 10.9  Patrol
tick  2: enemy  1.5  player 10.8  dist  9.8  Patrol
tick  3: enemy  2.0  player 10.2  dist  8.7  Patrol
tick  4: enemy  2.5  player  9.6  dist  7.6  Patrol
tick  5: enemy  3.0  player  9.0  dist  6.5  Patrol
tick  6: enemy  3.5  player  8.4  dist  5.4  Patrol
tick  7: enemy  4.5  player  7.8  dist  4.3  Chase
tick  8: enemy  5.5  player  7.2  dist  2.7  Chase
tick  9: enemy  5.5  player  6.6  dist  1.1  Attack
tick 10: enemy  5.5  player  6.0  dist  0.5  Attack
tick 11: enemy  5.5  player  5.4  dist  0.1  Attack

Read the log as the enemy's diary: seven ticks of patrolling, a switch to Chase as soon as the distance drops to 5 or less, and Attack once it's within 1.5. Notice that the distance printed each tick is measured before the enemy acts, which is exactly what the enemy's decision was based on.

Even a 12-line AI is hard to reason about from the code alone. Print or draw what the AI thinks every tick (state, distances, targets). You'll see this habit in every lesson, and the Debugging AI lesson turns it into tools.

What makes game AI different

  • It's a performance. Players judge AI by what they see and hear. A guard who shouts "Who's there?" before searching seems smarter than a silent one with a better search algorithm.
  • It runs on a budget. A 60 FPS game has 16.6 ms per frame for everything. AI often gets 1–2 ms for all agents, so expensive work (paths, line-of-sight rays) is cached, spread over frames and limited per frame.
  • It must be beatable and readable. Enemies telegraph attacks, miss the first shot, and give up the chase eventually. Perfect aim is easy to write and no fun to play against.
  • Designers tune it. Ranges, timers and weights live in data, not code, so designers can adjust them without a programmer.

How the course fits the track

This course assumes the game loop and basic state machines from Game Development Roadmap & Fundamentals and the vectors and dot products from Math for Games. The pathfinding section builds on graphs and priority queues; if they are new to you, Data Structures & Algorithms covers them in depth. Afterwards, apply the ideas in Unity, Unreal Engine 5 or Godot.

Tip: Read the C# models as pseudo-code you can run. Engines give you navmeshes, behavior tree editors and perception systems, but they all implement these same algorithms underneath. Knowing the model is what lets you debug the engine's version.

Test yourself

What is the main goal of most game AI?

  1. To play as well as possible
  2. To make the game fun and characters look believable
  3. To learn from the player with neural networks
  4. To use as much CPU as is available

Why do game AI programmers print or draw an agent's state every tick?

  1. Because engines require it
  2. Because even small AIs are hard to reason about from the code alone
  3. To slow the game down
  4. To save memory

Which section of this course answers the question 'which way gets me there?'

  1. Decisions
  2. Movement
  3. Pathfinding
  4. Perception