Artificial Intelligence

Delving into coding AI in Unity with dynamic expanding behavioural trees, and their interactions within the world that solidifies its realism.

AI Image


When designing the AI within an RTS, simulation, or action game, a large part of the consideration is seemingly perfecting the feeling of realism within the behaviour of each unit, whilst preventing a level of complexity and unpredictability that may frustrate the player. This often results in either major simplification of the player's core mechanics, or the AI's. This can sometimes be a good thing, tailoring the players experience and allowing each of their actions to be clear in terms of how it impacts the game, meaning the player can adapt their playstyle in interesting ways, but can obviously become more complex as you dive deeper within. Below I'll quickly cover how to implement simplistic AI within Unity, and how to take that behavioural arc and create enemies or allies that feel alive within the world.


AI Image

To start, we can assign each unit a simple line of sight to detect and keep in their array what object or enemy they encountered. Each units ability to observe their environment within the world is based on this line of sight radius. There can also be a simplified audio check that allows the AI to turn in the correct direction, allowing them a simulated intelligence and awareness.

Taking this forward, each unit can also have a simple hive-mind mechanic joined within their kingdom or allies that allows basic values and inputs to be passed on, meaning wood will be collected if needed, or if suddenly attacked, will call back assistance. This further adds to the predictability and level of understanding that the player will be able to pick up over time, with the first step learning the limits of the unit within the game world, to then impacting its behaviour in a more subtle manner.


Upon first tackling AI within an RTS, I decided to break it down by assigning distinct behaviours that each unit cycled between varying from; Neutral, Questing, Inquisitive, Aggressive, and Defensive. Each state contains its own behaviour tree assigned to that unit and can be changed according to what situation that unit is currently dealing with, influencing certain elements of their game styles. The intention with having five distinct behaviours is the predictability this offers to the player and allows them to potentially play around and influence each unit much easier as they slowly learn what action they'll complete based on what instance of their behaviour tree they are currently within. Added on top is each units ability to interact within a believable sense within the kingdom's economy with their purchasing of potions and armour before venturing out on a quest, further adding to that level of realism.

AI Image

This ideology carried over to my third-year project Abtohka and allowed me to create an advanced 2D AI that can be experienced by playing the game.


Within Abtohka: Kingdoms of Despair, each Traveller has set stances available to them. Stances are AI characteristics that can be assigned to each playable character when not being controlled by the player. This allows the player to then influence certain elements of that Traveller’s playstyles and technical behavioural trees, without directly controlling their actions, allowing for predictable variations of each Traveller through how they interact with environments, puzzles, and adversaries, in a familiar manner.

AI Image

The Stances also allow each Traveller to act within a moderately complex range of behaviour, that abides by the player’s wishes, whilst also replicating intelligent AI. This prevents unpredictable or unwarranted behaviour that a dynamic branching AI behaviour tree might otherwise possess - particularly in a puzzle game where the progression of puzzles relies heavily on the player’s ability to adapt and problem solve. Below is a brief breakdown of what each Stance does within its respective game boundaries. The Stances are Attack, Support, Guard, and Investigate.


Influences the currently stanced Traveller to engage within their own behavioural tree to value combative and aggressive situations over more passive or alternative routes. The neutral behaviour of Attack allows the Traveller to simply follow the player, while no adversaries are within the four Travellers line of sight. While in the neutral behaviour, no formation or tactical ideology will be prioritised, instead, a seemingly random path would be calculated to keep the Travellers somewhat within the range of the active Traveller. However, once an enemy enters the LOS, the Travellers in Attack Stance will actively engage with the closest adversary to their current location (within their LOS array) unleashing the main force of their firepower almost instantaneously as they opt to destroy their opponent as fast as possible before resuming their neutral behaviour. Travellers in the Attack Stance will only ever use their abilities when engaging with combat, they will also opt to not exhaust their resources for as long as possible, however, unlike in the Support Stance, running out of energy in Attack is possible, and will require the player to keep that in mind.

AI Image

In this example, the Oracle and the Brute are both placed within the Attack Stance with almost nothing to do, so they follow the traveller the player is currently controlling; the Warden. However, upon the Oracle encountering an enemy within her LOS, she immediately goes to attack it, with the Brute then following closely behind.


Allows the currently Stanced Traveller to engage within the environment with a more tactical mindset, utilising calculated and smart movement to avoid enemy interaction whilst engaging within a set formation depending on what stance each other Traveller currently is in. Each Traveller within the Support Stance will focus their abilities and attention in further drawing out the players strengths through various tools ranging from healing, to keeping adversaries away through usage of their abilities. Whilst within the Support Stance, its feasibly impossible for any of the Travellers to run out of resources, opting to instead ceasefire and back off upon reaching a set threshold, resulting in this Stance becoming the default level roaming characteristic for skilled players.

AI Image

In this example, the Oracle, Brute, and Warden are all in the Support stance, choosing to ignore the enemy, and form a formation around the currently controlled Traveller, which is the Oracle. This formation depends on what Traveller is being controlled, as well as the list of Travellers currently in the Support Stance. In this example, the Oracle is being controlled, and as such the Warden goes in front of her to absorb the damage, while the Brute defends behind her; both Travellers ready to utilise their abilities if the need arises.


Forces the Traveller to remain in said position, locked in place, until either controlled or swapped out of this Stance. Despite the AI being “off” whilst in this stance, they will still attack and defend themselves once an Adversary enters their LOS, defending said spot with their life with a visual reminder to inform the player that they’ve engaged in combat.


The only non-combat oriented stance, Investigate, allows the character to wander within an already preexplored game space with a predefined approach to exploring both narrative elements and hidden gameplay elements such as doors, secret narrative components, and key items littered throughout the world. Investigate can also be used for puzzle purposes, dropping hints and advice towards the player that could make solving a potentially difficult puzzle that much easier. However, upon encountering conflict such as an enemy or an impassable section, the Traveller will then return to the player and follow them cautiously. Upon one of the Travellers accidentally entering a combat phase, the game will enter the Stance selection screen, slowing down time and allowing the player to plan out their next moves wisely.

AI Image

In this example, the player opted to put the Apostle into the Investigate stance as they proceeded to continue exploring the environment. This allowed the Apostle to then find a hidden item that the player walked past earlier, bringing it back to the player before once more returning to their Investigatory stance, and continuing their search to the closest secret to their position. To experience the technical design and implementation in practise, play Abtohka: Kingdoms of Despair.


Planning out what you want each action the AI to do is vital when moving on to the implementation stage. Upon deciding what it is that you want the AI to do, and having both a LOS and some form of movement, creating the AI can be as simple as telling the AI where to go, and watching the AI run, attack, or interact within that space.

AI Image

Now, this might not be as easy as I just mentioned, with prior checks and instances seemingly coming out of thin air. It's important to understand the dynamics within your game and plan the AI around that, but this is definitely an easy start that helps ease you into creating AI, and I found the concept of having distinct stances much easier to plan around, even if I were to replicate it and not allow the player to control them, I'd adopt the stances automatically based on certain values within the game world.

Overall, this was an interesting variation in creating AI to experiment with, deviating from my usual RTS or simulation units that go out collecting resources and expanding their kingdom. It was fun to explore how each of these distinct personalities can interact within this tightly constructed space, and how each of their interaction with the player can further add to the enjoyment of the experience.

Go Back