Designing Artificial Intelligence
Technical, System and AI Design · 15 minute read
Delving into coding AI in Unity with dynamic behavioural trees and their interactions within the game world that solidifies their realism.
Designing Artificial Intelligence
To experience the design and implementation in practise, try Abtohka: Kingdoms of Despair for free.
Part 0: Theory
When designing and implementing AI within an RTS, simulation, or action game, a large part of the consideration is perfecting the feeling of realism within each character's behaviour whilst preventing a level of complexity and unpredictability that may frustrate the player. This, of course, varies from genre to genre, yet often results in either the major simplification of the player's, or the AI's core mechanics. Tailoring the player's experience and allowing each of their actions to be clear in terms of how it impacts the game is vital, and designers need to approach AI with that perspective, planning out how they want their AI to function and force the player's to adapt their playstyle in interesting ways. Below I'll quickly cover my design process for implementing simplistic AI within Unity, and how to create enemies or allies that feel alive within the world of the game. This post will be less about how to program AI yourself, and more about the design process.
Part 1: Line of Sight and Pathfinding
The first thing that any traditional AI requires is both a LOS, and some form of Pathfinding. We can assign each unit a simple line of sight to detect and keep in their array what object or enemy they encountered,
and 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 of most PVE games being to learn the limits of the AI, to then impacting their behaviour.
Part 2: Behaviour Trees
Upon first tackling AI, I decided to break it down by assigning distinct behaviours that each unit cycled between, varying from; Neutral, Inquisitive, Aggressive, and Defensive. Each state contains its own behaviour tree and can be changed according to what situation that unit is currently dealing with, influencing certain elements of their game styles. This offers the player the potentiality to play around and influence units much easier as they slowly learn and understand how each stance affects each action. As I expanded upon this system, I added more creative behaviours such as Questing, allowing units to interact in a believable sense within the kingdom's economy with their purchasing of potions and armour before venturing out on a quest, adding to the realism.
This ideology carried over to my third-year project Abtohka and allowed me to create an advanced 2D AI.
Part 3: AI within Abtohka
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.
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 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 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.
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 the 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.
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 explored 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 conflicts 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 downtime and allowing the player to plan out their next moves wisely.
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.
Part 4: Implementation
When creating AI, planning out each action is vital before moving on to implementation. A large error young developers make is aiming to have complex incredibly intelligent AI that will do everything a person does whilst firing lasers and doing backflips, take a step back and think, what core elements are vital for your AI to have, and then slowly expand. Even games with incredible vibrant worlds and stories such as World of Warcraft have enemies that patrol, roam, attack, and flee. The complexity comes from the combat, and no player thinks about the AI's limited moveset because the world and core mechanics are tailored towards that experience. 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.
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. 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 and just keep them hidden, somewhat similar to aggroing mobs in any MMO with them
going from peaceful, to angry - chasing the player, and finally to either dying or losing aggro if the player gets too far.
I hope you enjoyed reading this post, and have some interesting variation in creating AI to experiment with. It was fun to explore how each of these distinct personalities can interact within this tightly constructed space, and how each of their actions with the player can further add to the enjoyment of the game experience.