# Enemy AI

***

## Overview

At its core, the enemy AI is designed around **states** and **behaviour rules** defined in a central configuration asset. The AI controller reads this configuration to determine whether an enemy starts in a patrolling, roaming or idle state. From there, a behaviour tree controls the transitions between tasks such as patrolling, chasing, roaming and attacking.

Attacks themselves are driven by the modular combat system: attacks are selected randomly from the enemy’s configuration based on their **weight** and **cooldown**, preventing spam. A notify state within the animation toggles hitboxes on and off to ensure that only the correct frames deal damage. The system is **interface‑driven** and comes with a behaviour‑tree task for attacking, allowing it to integrate seamlessly into the AI workflow.

{% hint style="info" %}
If you are not familiar with the Unreal Engine AI Framework, or the general idea of pawns/characters -> controllers & behaviour trees/blackboards, we highly recommend watching the video below before you continue any further. It's a short, simple but very helpful guide to AI from Epic Games. This should help you get started.
{% endhint %}

{% embed url="<https://www.youtube.com/watch?v=iY1jnFvHgbE>" %}

***

## Key Concepts

The enemy AI is built around a set of core systems that work together to provide intelligent and modular behaviour. <mark style="background-color:green;">At the center lies a custom</mark> <mark style="background-color:green;"></mark><mark style="background-color:green;">**AIController**</mark><mark style="background-color:green;">, which is responsible for launching the behaviour tree and coordinating communication</mark> between movement, perception, combat, and health components. It reads the assigned enemy configuration to determine the initial state and updates blackboard values whenever perception events occur.

The <mark style="background-color:green;">actual decision-making is handled by the</mark> <mark style="background-color:green;"></mark><mark style="background-color:green;">**behaviour tree**</mark>, where tasks such as “Patrol”, “Roam”, “Chase”, and “Attack” are combined to form fluid state machines. Combat is seamlessly integrated through the *Attack Behaviour Task*. This task retrieves the combat component from the pawn, requests an attack based on the configured weights and cooldowns, and waits for the attack-finished event before returning control, ensuring that attacks are synchronised with the underlying animation and combat system.

To detect targets in the world, <mark style="background-color:green;">the AI makes use of</mark> <mark style="background-color:green;"></mark><mark style="background-color:green;">**perception**</mark> <mark style="background-color:green;"></mark><mark style="background-color:green;">through Unreal’s AI Perception component. It can sense players via sight, hearing, or custom senses.</mark> When a target is detected or lost, the controller updates the blackboard accordingly, causing the behaviour tree to transition between states — for example, from patrolling to chasing.

The **Environment Query System (EQS)** <mark style="background-color:green;">provides the AI with the ability to find dynamic positions in the level, such as random roaming spots or tactical cover locations.</mark> Behaviour-tree tasks can execute EQS queries to choose suitable destinations and move the pawn there. These queries can be customised on a per-enemy basis, allowing different enemy types to behave uniquely within the same framework.

Finally, <mark style="background-color:green;">all settings can be configured in a single config asset.</mark> This configuration is then added to the character blueprint of your enemy and, with the help of blueprint interfaces, propagated to all the relevant systems. <mark style="background-color:green;">As the entire AI is heavily data-driven, you can create as many configurations as you like and switch between them with just a few clicks.</mark>

***

## Quick Start

Setting up an enemy is a straightforward process and can be done in just a few steps. Every enemy must first be assigned an **Enemy Config** asset. This configuration defines the default state of the AI — whether the enemy begins patrolling, roaming, or idling — and also references any attack configurations that should be available during combat. <mark style="background-color:orange;">Failing to assign a configuration asset will result in undefined behaviour!</mark>

Once the configuration is in place, you can rely on the **provided behaviour tree**, which already includes tasks for patrolling, roaming, reacting to perception events, and executing attacks. The default tree can be duplicated and customised to introduce additional states or to adapt existing behaviours to your project’s needs.

<mark style="background-color:green;">To make sure your enemies interact with the world as intended, it is important to</mark> <mark style="background-color:green;"></mark><mark style="background-color:green;">**tune the perception component**</mark> <mark style="background-color:green;"></mark><mark style="background-color:green;">on each character.</mark> Parameters such as sight radius or field of view determine how far and how wide the AI can detect players and other relevant actors.

Finally, for roaming behaviour or more advanced tactical movement, you can **create EQS queries** within your project. These queries can be referenced directly from the behaviour tree and allow the AI to pick suitable destinations in the environment, ensuring more dynamic and varied movement patterns. <mark style="background-color:green;">The Enemy AI - Toolkit already comes with premade</mark> [<mark style="background-color:green;">Environment Query System</mark>](https://labgames-studio.gitbook.io/basic-enemy-ai/documentation/enemy-ai/environment-query-system) <mark style="background-color:green;">Solutions.</mark>

***

## Next Steps

The overview above gives you a general idea of how the enemy AI is structured and how its main systems interact. To make full use of the toolkit, <mark style="background-color:green;">it is helpful to understand each part in more detail</mark> and see how they can be customised for your own project. The following sections break down the most important building blocks of the AI, explain their responsibilities, and show how they fit together inside the behaviour tree.<br>

* [Controller](https://labgames-studio.gitbook.io/basic-enemy-ai/documentation/enemy-ai/controller)
* [Behaviours](https://labgames-studio.gitbook.io/basic-enemy-ai/documentation/enemy-ai/behaviours)
* [Perception](https://labgames-studio.gitbook.io/basic-enemy-ai/documentation/enemy-ai/perception)
* [Environment Query System](https://labgames-studio.gitbook.io/basic-enemy-ai/documentation/enemy-ai/environment-query-system)
* [Config](https://labgames-studio.gitbook.io/basic-enemy-ai/documentation/enemy-ai/config)
