In many single-player games – and some multiplayer games – a form of artificial intelligence (AI) is at work.
Some of these AI’s are very rudimentary, such as the “AI” that controls the opposing padel in Pong whose only goal is to bounce the “ball” back to you. Others are a lot more complex, such as enemies you might find in games like Fire Emblem who need to have complex strategy algorithms. Regardless of the complexity, though, there is no questioning that without AI, we would not have the beautiful fabric of games that are available to us today.
Unsurprisingly, learning how to add AI to your games is a challenging but rewarding task. And, we find there’s no better place to learn AI than with the popular Unity engine, which comes with several helpful tools for setting up our AIs. In this article, we’re going to showcase some of the best Unity AI tutorials available so you can start adding this great feature for your own games.
Let’s dive in!
What is Artificial Intelligence?
Before we get to the tutorials, we did want to take a moment to define the scope of artificial intelligence. This way, new game developers can get on the same page.
Artificial intelligence can be defined as a system of instructions that allow computers to replicate tasks that would normally require a human. This isn’t dissimilar to coding itself, which is just sets of instructions and rules for the computer to follow. With AI, however, we’re focused more so on mimicking human intelligence. This comes in many forms such as speech recognition, visual identification, and, most importantly for games, decision-making.
In the realm of games, we most often associate AI with programmed enemy characters. These characters have to be able to make decisions, whether that’s navigating terrain, deciding how to attack you, and so forth. However, other things like speech recognition have become more common as well as our technology has improved. Regardless, AI programming is all about instructing computers “how” to decide how tasks should be performed, and then performing those tasks to suit a pre-defined situation.
Within AI, though, there is one more concept we want to address: machine learning. Machine learning is a subfield focused on creating machines that can take in data and “learn” to perform the tasks we want them to better. In fact, things we mentioned already, like visual identification, are generally built up through machine learning. This principle works through sheer pattern recognition, which machines are even better at than humans.
Though machine learning is a lot newer when it comes to games, it still has shown a lot of potential for allowing us to create even smarter opponents, among other things. As such, we will be including machine learning elements on this list!
FULL 3D ENEMY AI in 6 MINUTES!
Duration: 6 Minutes
This Unity tutorial by Dave / GameDevelopment is a fantastic – if not quick – jumpstart into the topic of AI enemies. Without getting too complex, the tutorial will show you how to set up three states for an enemy AI: patroling, chasing, and attacking. All three states operate on the simple parameter of how close the player is to the enemy character. Based on proximity, the AI is designed to choose the appropriate state, which ultimately controls its action.
In this case, an enemy character will move around randomly with patroling. When the player is close enough to be “seen”, the enemy will chase the player. If the player is close enough to be attacked, the enemy attacks. While a very simple decision tree, it is a fantastic start for beginners who want to experience a taste of how AI decision-making works for games.
In addition, this tutorial also covers a bit of NavMesh: Unity’s component for pathfinding. As such, without going too deep, you’ll learn a bit about how enemies decide just where they can actually go.
Create a simple AI with behaviour trees
Duration: 23 minutes
Created by Mina Pêcheux, this Unity tutorial takes a more in-depth look into how a finite state machine is structured. Before even touching Unity or any C# coding, the tutorial talks extensively about behavior trees. These behavior trees, which essentially map out the behavior we want objects to take and when, form the design basis for implementing our AI in code. Additionally, it allows you to explore the limitations of finite state machines, and how to plan around those limitations when creating your enemies.
After theory, though, the tutorial does jump into Unity and shows you how to adapt the behavior tree you design to actual C# code. The tutorial uses the examples of a patroling state, a targeting the player state, and an attacking state to demonstrate this. On top of this, the tutorial explains fairly comprehensively how the actions associated with the states – i.e. the behavior – is implemented.
This is also a fantastic tutorial if you’re at all interested in working with animations, as these too can be controlled via the behaviors.
The Ultimate beginner’s guide to AI with Unity & C#
Duration: 20 minutes
In this tutorial by Blackthornprod, you’ll once again get the chance to explore some basics of AI development. This includes things such as how to create state machines, how to add behaviors to your states, and similar. However, this tutorial differs from some of the other beginner-friendly tutorials in two important ways.
First, this tutorial is focused more so on 2D games. 2D games have their own little quirks when it comes to implementing behaviors – particularly in the realm of movement. As such, this tutorial will teach you how to recognize those considerations. This is particularly important when it comes to pathfinding which can take a bit of a different form compared to 3D.
The second difference is that this tutorial is focused less on state machine principles, and focused more so on getting you up and running with some of the most common and useful behaviors. This includes being able to follow players, being able to flee from players, being able to attack players, and being able to walk around randomly. Additionally, you will get to explore 2D pathfinding, as well as line of sight – both very important and common concepts to deal with when it comes to triggering behaviors.
A.I State Machine Made EASY
Duration: 8 minutes
If you’re a bit confused by the concept of state machines, this tutorial by Sebastian Graves is for you. Whereas a lot of tutorials on this list focus on programming the behaviors themselves along with the state machine, this tutorial focuses on the state machine aspect almost exclusively.
State machines, for those who don’t know, are basically AI machines that control different AI behaviors. When you want an object to exhibit a certain behavior, you change its entire state to execute that behavior, and by consequence, use states to store those behaviors. With this tutorial, you’ll explore the proper C# scripting methods in full for setting up a state machine. You’ll discover not only how these state machines are structured, but how to smoothly switch between states.
Plus, you’ll also learn how to build a state machine fairly independently of the behavior. In so doing, you’ll discover how to quickly adapt your state machine to incorporate the behaviors you personally need for your game project without getting confused by what those behaviors actually do.
NPC AI and Combat for Survival Games
Duration: 1.8 hours
One of the few premium resources on this list, this course by Zenva covers not just AI mechanics, but how to incorporate your AI with other sorts of game mechanics.
You’ll start off by exploring the creation of state machines to add wandering, fleeing, and attacking states for NPCs. However, unlike previous tutorials on this list, you’ll do so in a way that is more modular – so you can assign different NPCs to use different combinations of those states. Along with this, you’ll also learn to work with the NavMesh component for dynamic pathfinding.
As mentioned though, this particular resource is great if you want to then take the next step in further integrating AI with the rest of your game. You’ll discover how to implement things such as combat and loot along with your AI controlled enemies. To add to this, you’ll also combine your programmed state machine with the Unity Animator (which is, in itself, a state machine) to get your NPCs moving in tandem with their behaviors. All in all, this course is a total package if you’re looking for a bit more oomph to learning AI programming.
Unity NavMesh Tutorial – Basics
Duration: 12 minutes
Although this has been featured a bit above, this tutorial by Brackeys should be your go to if you’re mainly interested in AI pathfinding the easy way using Unity’s NavMesh.
The Unity NavMesh component is a tool that allows you to take your terrain and turn it into a static “navigation mesh”. This navigation mesh marks out the walkable areas of your level. After which, you can then create a NavMesh Agent which is able to work with this navigation mesh to determine how to reach a set point. In other words, you don’t have to code anything complicated for the NavMesh to figure out how to get an object somewhere – Unity handles everything for you.
This tutorial will cover all those basics comprehensively, including how to set up your navigation mesh quickly and easily, as well as how to adjust settings that come as part of the component. You’ll also learn how to create the NavMesh agent, resulting in a quick project that lets you click on a spot and have an object travel to it.
Sync Animator and NavMeshAgent States
Duration: 10 minutes
For fans of automated approaches to handling AI states in Unity, this tutorial by Llam Academy is a great one to check out to see just how much control you can have.
In this case, the tutorial focuses on two separate systems within Unity: the Unity NavMesh Agent which deals with pathfinding and the Unity Animator which deals with when and how to play animations. Though both aspects have been covered in previous tutorials, this one takes a more thorough and intermediate approach for those who aren’t afraid of things like events or non-primitive models.
The tutorial first shows you how to set up an Animator state machine, though with a big focus on creating parameters along with each state. Then, using C#, you’ll explore how to not only set up your NavMesh Agent, but sync it more comprehensively to work with the Animator states automatically. You’ll even get to see it in action with animations like jumping, giving you insight into how you can keep your characters dynamic even with things like AI.
How to use Machine Learning AI in Unity! (ML-Agents)
Duration: 45 minutes
Wouldn’t it be great if our AIs could just figure things out for themselves? Well, this tutorial by Code Monkey will show you how possible that it.
As mentioned at the start, machine learning is a subset of AI focused on machines being able to take in data and improve how they process a task to reach a desired goal. Though it is newer, within Unity we have the ability to access this with ML-Agents. The tutorial featured here is an in-depth study of those ML-Agents, how they work, and how to use them.
Besides setup, which is a very involved process, you’ll learn a few key characteristics about working with ML-Agents and using reinforcement learning. This style of learning involves programming your ML-Agents with actions, giving them the ability to make observations, allowing them to make decisions around the programmed actions, and rewarding them when specific results are obtained. In addition, you’ll also cover how these steps allow you to “train” your agent and, based on results, improve your models.
An Introduction to Unity’s ML-Agents
Duration: 12 minutes
Given how complicated machine learning is, we also wanted to include this tutorial by Tim Bonzon.
Like the previous tutorial, this one focuses on using ML-Agents in Unity with a reinforcement learning style approach. You’ll of course learn the important aspects of creating ML-Agents with actions, decision-making abilities, rewards, and so forth. Likewise, the tutorial also covers how to train your agents so they can get better at the assigned task.
Where this tutorial shines is its practical approach. If you’re not as interested in the theory parts of machine learning and just want a project up and running, this is a good tutorial for that. The tutorial focuses almost solely on building the project – teaching the computer to balance a ball on a moving platform. Thus, like the machine, you’ll really learn by doing the task itself and picking up hands on how reinforcement style learning is implemented with ML-Agents.
Bolt Tutorial – Game AI for Creatures in Unity
Duration: 13 minutes
Insofar, all the chosen tutorials have had a focus on C# scripting – with a few Unity tools and components here and there. This tutorial by Home Mech shows you that visual scripting with the popular Bolt package is also possible when it comes to AI in Unity.
Using not a single piece of manual code, the tutorial will show you how to build a state machine using Bolt’s drag and drop nodes, featuring a resting state and an evading state for a little frog. You’ll also learn how to “program” transitions between these states, just as you would with regular C# scripts.
Besides this, the tutorial does also cover how to set up behaviors along with those states. This includes the complicated matter of making the frog move during its evasion state. The state pertains to ideas such as how 3D space movement works, how to make the frog do things like hop and flip, and how to use SLERP to make smooth movements as the frog moves.
A* Pathfinding Tutorial (Unity)
Duration: 3 hours, 11 minutes
This tutorial series by Sebastian Lague is probably the most advanced on the list. However, we want to include it if you’re looking for a bit of a challenge in programming pathfinding AI.
A* is an algorithm in computer science that was originally designed for traversing regular old graphs. As time has passed, it’s become a rather common method for pathfinding in games, though. Without getting to in-depth, it allows the computer to calculate the shortest distance to a specific point using a complex formula involving calculating costs of moving to each space. This works even for levels with obstacles, as they can be accounted for within the A* setup.
The series focuses very in-depth on this topic. You’ll first learn how the algorithm works, how to divide your levels into “grids”, and how to implement the algorithm with C#. You’ll then get the chance to explore more intermediate topics, such as making movement smooth or giving different terrain movement penalties to consider. For super advanced users, you’ll also learn how to use multi-threading to improve performance, which is imperative with this style of pathfinding.
Language Recognition AI with Unity and Azure
Duration: 1 hour, 30 minutes
Last but not least, we have another premium resource from Zenva. In this course, you’ll take a bit of a different approach to machine learning by not programming that part at all.
To elaborate, this course focuses on using Microsoft’s Cognitive Services. These services provide pretrained models capable of certain tasks, such as image recognition or, in this course’s case, speech recognition. Though these solutions are customizable to many extents, they allow you to add AI to your projects much more easily, since much of the tedious groundwork is already done.
Through this course, you’ll work with the speech recognition aspect of these services to build a voice commanded rover for exploring planets in Unity. This will involve discovering how to set up which commands should be looked for in the game, but also how to tie those commands to an action. You’ll also learn this in ways that can be applied to many other projects where you want to save a bit of time on your AI programming.
While there are surely more tutorials on AI out there, this collection of best Unity AI tutorials should get you started. We’ve tried to include a little bit of everything – from NavMesh to state machines to even speech recognition. However, there is always more to discover in the realms of AI, and each game will require some different decision-making processes. Nevertheless, AI is a powerful tool that can add a lot of replay value and challenge to your games.
So, regardless of what you’re looking to build, we wish you the best of luck with adding AI to your projects!
FINAL DAYS: Unlock 250+ coding courses, guided learning paths, help from expert mentors, and more.