Building Game AI - Pathfinding, Decision Trees and Behavior Systems

When players say a game feels smart what they usually mean is that enemies move believably, make reasonable choices and react to the player in interesting ways. You do not need deep learning models to get there. For most indie and mid-scope projects a mix of pathfinding, decision logic and behavior systems is enough to create convincing AI that ships.

In this guide you will learn how these pieces fit together plus concrete patterns you can adapt in engines like Unity, Godot and Unreal.

Who This Is For

This article is for you if:

  • You are a game programmer who has written some gameplay code but feels unsure how to structure AI.
  • You are a designer or technical designer who wants better conversations with programmers about AI behavior.
  • You are building a portfolio piece and want one or two AI systems that look solid in a reel.

If you are just getting started with engines and scripting also check out the posts on Game Development with Lua, Game Development with JavaScript and the Unity Game Development Course in the courses section so you have a solid foundation.


The Three Pillars Of Practical Game AI

Most shipped games use a combination of three ideas.

  • Pathfinding – How characters move from A to B while avoiding walls and obstacles.
  • Decision logic – How they pick what to do next such as chase hide reload or retreat.
  • Behavior systems – How those decisions are organized over time so AI feels coherent rather than random.

You can mix and match techniques inside each pillar but the overall shape stays the same across genres from stealth games to action RPGs.


Pathfinding - Getting Characters From A To B

Before AI can act smart it needs to reach interesting places.

Common Approaches

  • Grid based pathfinding – World is a grid of nodes. Easy to visualize and great for tile based games and prototypes.
  • Navmesh based pathfinding – World is represented as polygons that mark walkable space. This is what Unity NavMesh and Unreal navigation use.
  • Waypoint or graph based pathfinding – You place nodes manually and connect them with edges. Useful when you need precise control over paths.

Under the hood most engines use A star search or a close variant. You rarely need to implement A star yourself but it helps to understand its constraints.

Practical Example - Simple Chase Behavior

Engine agnostic pseudocode for a basic chaser that uses navmesh or grid pathfinding.

def update_ai(dt):
    if not can_see_player():
        patrol()
        return

    target_position = player.position
    path = navigation.find_path(self.position, target_position)

    if path:
        follow_path(path, dt)

Key ideas:

  • Keep navigation separate from decision logic. Code that finds paths should not decide whether to chase.
  • Cache or reuse paths when possible instead of recomputing every frame.

For concrete implementation details:

  • Unity NavMesh – see the official NavMeshAgent documentation.
  • Godot navigation – see NavigationServer3D and NavigationAgent3D docs.
  • Unreal – check out UNavigationSystemV1 and AI Move To nodes in Blueprints.

Decision Trees - Choosing What To Do Next

Once your agents can move they need to decide what action makes sense in the current situation. Decision trees are a simple and powerful tool for this.

What Is A Decision Tree In Games

A decision tree is a nested set of questions that narrow down to an action. For example.

  1. Is the player visible
    • No → Patrol
    • Yes → Continue
  2. Is health low
    • Yes → Retreat
    • No → Continue
  3. Is player within attack range
    • Yes → Attack
    • No → Chase

The result is a single action that your AI should take this frame or this tick.

Implementing A Simple Decision Tree

You can implement a tree with plain if statements or with small data structures. Here is a minimal code style version.

def choose_action(context):
    if not context.player_visible:
        return "PATROL"

    if context.health < 0.3:
        return "RETREAT"

    if context.distance_to_player < context.attack_range:
        return "ATTACK"

    return "CHASE"

Then your update loop becomes.

def update_ai(dt):
    context = gather_context()
    action = choose_action(context)

    if action == "PATROL":
        patrol(dt)
    elif action == "RETREAT":
        retreat(dt)
    elif action == "ATTACK":
        attack(dt)
    elif action == "CHASE":
        chase(dt)

This pattern is easy to debug and great for portfolio pieces because you can show the tree in diagrams or comments when walking through your code.


Behavior Systems - Making AI Feel Coherent

Decision trees are great for picking a single action but you also need to think about state over time. Two common approaches are finite state machines and behavior trees.

Option 1 - Finite State Machines

A finite state machine defines a current state and a set of rules that describe how to move between states. For example states could be Idle Patrol Alert Chase Attack Flee.

Key advantages.

  • Easy to understand and visualize.
  • Works well for small numbers of states.

Drawbacks.

  • Can become messy spaghetti if you keep adding states and transitions without structure.

Option 2 - Behavior Trees

Behavior trees organize behavior as a hierarchy of tasks with nodes like Sequence Selector and Parallel. They are widely used in modern games for organizing complex NPC behavior.

High level concepts.

  • Sequence node – Runs children in order until one fails.
  • Selector node – Runs children in order until one succeeds.
  • Leaf nodes – Concrete actions or checks such as MoveTo or IsPlayerVisible.

You do not have to build a full generic framework to get value from the idea. Even a small hand coded tree for a boss can keep your logic cleaner than a huge pile of if statements.


Putting It Together - Example Enemy Design

Imagine a ranged enemy in a third person action game.

  • It should patrol when it has not seen the player for a while.
  • It should move to cover rather than standing in the open.
  • It should retreat when nearly dead.

One way to wire this up.

  1. Use pathfinding to move between patrol points and cover spots.
  2. Use a decision tree to choose between Patrol SeekCover Attack and Retreat.
  3. Use a state machine or behavior tree to manage transitions such as Alerted or Fleeing so the enemy does not rapidly flip between actions every frame.

As you implement this write down a few design rules such as minimum time spent in a state or cooldowns before repeating an ability to keep behavior readable.


Pro Tips And Common Mistakes

Pro Tips

  • Start with behavior that serves the game design not what sounds clever technically.
  • Implement AI with tunable parameters so designers can tweak distances timers and probabilities without code changes.
  • Add simple debug visualizations such as drawing current path or field of view cones.

Common Mistakes To Avoid

  • Building a giant generic AI framework before you ship a single enemy.
  • Mixing movement code and decision code in the same functions so you cannot change one without breaking the other.
  • Forgetting to handle edge cases like unreachable targets or blocked paths which leads to stuck enemies.
  • Making AI perfectly optimal rather than fun. Real players enjoy enemies that sometimes make mistakes or take risks.

How This Fits Your Portfolio And Career

Strong AI examples can be a highlight in a game development portfolio. When you show recruiters or hiring managers your work they want to see:

  • Clear problem statements such as stealth guards that coordinate searches.
  • Simple diagrams of decision trees or state machines.
  • Short video clips where AI behavior is easy to understand.

Pair this post with portfolio guidance from the Create a Game Development Portfolio course and the blog post on Game Development Career Paths so you can explain your AI work in interviews with confidence.


Frequently Asked Questions

Do I need machine learning to build good game AI

Usually no. For most games traditional techniques like pathfinding decision trees behavior trees and utility systems are enough. Machine learning can be useful for specific features but it adds tooling and production complexity.

How expensive is AI for performance

The biggest costs are usually pathfinding and perception checks such as raycasts. Limit how often you update full decisions and paths. For many NPCs updating once every few frames is fine.

Which engine should I learn first for AI

Pick the engine that best matches your project goals. Unity and Unreal have rich tools and docs while Godot is fast and lightweight. The underlying AI ideas in this article transfer across all of them.

How can I practice without building a full game

Create small sandboxes focused on a single behavior such as a guard that patrols a room or enemies that kite the player. You can build and polish these in a weekend and add them to your portfolio.


Next Steps

  • Pick a single enemy or NPC in your current project and sketch a decision tree for it.
  • Implement that tree using basic pathfinding provided by your engine.
  • Add one small behavior system layer such as a simple state machine to control when the tree is evaluated.

Found this useful. Bookmark it for later and share it with a friend or teammate who is also wrestling with game AI design.