Artificial Intelligence In Unity

A practical guide to programming non-player characters for games.

Last updated 2022-01-10 | 4.6

- Design and program NPCs with C# in Unity
- Explain how AI is applied in computer games
- Implement AI-related Unity Asset plugins into existing projects

What you'll learn

Design and program NPCs with C# in Unity
Explain how AI is applied in computer games
Implement AI-related Unity Asset plugins into existing projects
Work with a variety of AI techniques for developing navigation and decision making abilities in NPCs

* Requirements

* You should be familiar with C# and the Unity Game Development Engine.

Description

Do your non-player characters lack drive and ambition?  Are they slow, stupid and constantly banging their heads against the wall? Then this course is for you.  Join Penny as she explains, demonstrates and assists you in creating your very own NPCs in Unity with C#. All you need is a sound knowledge of Unity, C# and the ability to add two numbers together.

In this course, Penny reveals the most popular AI techniques used for creating believable character behaviour in games using her internationally acclaimed teaching style and knowledge from over 25 years working with games, graphics and having written two award winning books on games AI. Throughout, you will follow along with hands-on workshops designed to teach you about the fundamental AI techniques used in today's games.  You'll join in as NPCs are programmed to chase, patrol, shoot, race, crowd and much more.

Learn how to program and work with:

  • vectors

  • waypoints

  • navmeshes

  • the A* algorithm

  • crowds

  • flocks

  • animated characters

  • vehicles

Contents and Overview

The course begins with a detailed examination of vector mathematics that sits at the very heart of programming the movement of NPCs. Following this, systems of waypoints will be used to move characters around in an environment before examining the Unity waypoint system for car racing with AI controlled cars.  This leads into an investigation of graph theory and the A* algorithm before we apply these principles to developing navmeshes and developing NPCs who can find their way around a game environment.  Before an aquarium is programmed complete with autonomous schooling fish, crowds of people will be examined from the recreation of sidewalk traffic, to groups of people fleeing from danger. Having examined the differing ways to move NPCs around in a game environment, their thinking abilities will be discussed with full explanations and more hands-on workshops using finite state machines and behaviour trees.

The follow-along workshops included in the course come with starter Unity asset files and projects complete with solutions.  Throughout, there are also quizzes and challenge exercises to reinforce your learning and guide you to express your newfound knowledge.

At the completion of this course you will have gained a broad understanding of what AI is in games, how it works and how you can use it in your own projects.  It will equip you with a toolset to examine any of the techniques presented in more depth to take your game environments to the next level.

What students are saying about this course:

  • This has been my favourite Udemy-Unity course so far. It took me from literally 0% knowledge of how game AI is achieved, and took me to a whole new level. Waypoints, pathfinding, state machines, etc etc etc are all covered in-depth and will reveal the magic (spoiler alert: it isn't magic) behind making your computer characters seem like they really have a mind of their own.

  • Oh My God. I love her way of teaching things. I haven’t finished this course yet. But all i can say is that it is another brilliant course from her. Artificial intelligence by itself is a tricky thing to do. And before starting this course i never thought that i will understand anything in it. But i was wrong. With her style of teaching, you will learn how to move your characters in an ”intelligent“ way. This course is perfectly sliced and the pace is wonderful.

Who this course is for:

  • Anyone interested in learning how to program their own non-player characters (NPCs).
  • Anyone interested in seeing how artificial intelligence is applied in computer games.

Course content

11 sections • 71 lectures

Introduction Preview 03:26

This lecture introduces the course by outlining all the content that will be covered with a section by section breakdown.

Join the H3D Student Community Preview 01:26

H3D has a bustling online student community.  Here's how to get involved.

FAQ's Preview 00:14

Not So Scary Vector Mathematics Preview 07:02

In this lecture we will get an overview of vectors and why they are an important concept to have an understanding of before you launch into programming the movement of an NPC.

Vector Mathematic Basics Cheat Sheet Preview 04:41

Test your vector mathematics knowledge

Important Note Regarding Unity Updates to FBX Model Importing Preview 05:19

Unity uses the FBX file format by default to represent 3D models. Within Unity 2017 and greater the way the files are dealt with has changed slightly and may cause you to lose the textures from your models. Take note of the process in this lecture that will assist you in fixing this issue when you encounter it later in the course.

Vectors and Moving in a Straight Line Preview 11:50

In this lecture we will take a quick look at the nature of vectors and then discuss through a practical application how they can be used to move a character forward at a constant speed.

Traveling to a Goal Location Preview 12:21

Follow along in the lecture as we setup a goal location for the character to move towards. You will also be shown how to change the facing direction of a character such that it looks in the direction it is travelling.

Pushing the Character Forward Preview 12:40

In this lecture we will examine a different approach to moving the character. This time instead of setting a vector to travel along we will turn the character towards a goal and then push the character in a forward direction.

Slerping Preview 05:19

Instead of snapping a character around to face a goal in a single update you may want more smooth turning behaviour to occur that makes the movement appear more natural. In this lecture we will examine the slerp method for achieving this.

About Animation and Translation Preview 06:38

In this lecture we will take a quick look at the difference between animation translation and translating with code. Eitherway is acceptable and it will depend on what you are trying to achieve with the look of your character as to which one is more appropriate.

Waypoints Preview 09:10

In this lecture we will look at the simplest form of AI navigation: waypoints. Using the code we've developed thus far by giving the NPC an array of goals we can change its behaviour from chasing to patrolling.

Challenge Preview 00:13

Unity's Waypoint System Preview 12:38

If you import the Utilities from Unity's included assets you'll find code for setting up waypoints and navigating a circuit. In this lecture we will look at how to integrate these Unity asset's with our own code.

Car Racing with Waypoints Preview 13:25

In this lecture we will examine how to create a simple car NPC that can accelerate and brake while following a circuit tracker.

Customising Car Behaviours Preview 07:59

Continuing where we left the previous lecture we will add in another car and set about making the properties of the movement behaviour customisable.

Unity's Vehicle System Preview 15:38

Inside Unity's Standard Assets you will find the setup for a Waypoint following AI car with many customisable properties. In this lecture we will explore this feature as well as discuss how to add your own custom model into the system.

Graph Theory and Pathfinding Preview 08:31

Graph Theory underpins so many AI techniques and it certainly deserves some focussed attention. In this lecture we will examine the structure of graphs and several path searching algorithms will be discussed.

Pathfinding through Waypoints Preview 19:31

In this lecture we will apply the A* algorithm to finding a path through a system of waypoints converted into a graph.

Pathfinding through Waypoints Part 2 Preview 10:17

In part 2 of this hands-on workshop to put together a waypoint graph traversal system using the A * algorithm we will add UI buttons to send location commands to the NPC.

Challenge Preview 00:13

Waypoints in 2D Preview 01:05

This short article will explain how the code for dealing with 3D waypoints can be modified for 2D environments.

NavMesh Basics Preview 05:11

In this lecture we will modify the pathfinding exercise created thus far and introduce the concept of a NavMesh. This will reduce the amount of code we need to write as well as the time taken to setup a waypoint system manually.

From Waypoints to NavMesh Preview 09:07

In this lecture we will turn the previous waypoint system with the tank into a navmesh based project.

NavMesh Agents Part 1 Preview 17:42

In this lecture we will start taking a closer look at the navmesh agent component and explore the various settings for the baked nav mesh agent that controls how the navmesh is generated.

NavMesh Agents Part 2 Preview 10:32

Agents can be assigned different areas of the NavMesh through which to travel. In this lecture we will look at how to set up different areas with different costs and associate the agents with them.

Following a Player on A NavMesh and Setting-Up Off Mesh Links Preview 14:10

The test of a NavMesh is to try it out with a real character in a real situation. It's then you'll find glitches in the default settings and automatically generated paths. Without having to remodel the environment, off-mesh links can be generated to provide smooth paths were their aren't any. In this lecture we will make a character follow a first person player character around a map while tweaking the NavMesh to handle different situations when gaps and drops appear in the NavMesh.

Fixing Mixamo Textures Preview 00:09

This video link describes how to fix textures on imported FBX files from Mixamo.

Animating on a NavMesh Preview 13:36

In this lecture we will examine how the NavMesh agent code and be coordinated with character animation to provide smooth and believable movement of a character when pathfinding over a NavMesh.

Syncing Animation Speed with NavMesh Agent Speed Preview 06:35

In this short lecture I will demonstrate how to sync the animation speed with the nav mesh agent speed.

Multiple NavMeshes for Different Agent Sizes Preview 12:41

In order to include multiple navmeshes to cater for multiple different navmeshagent sizes we need to turn to some beta code that isn't currently included in Unity but available for download and inclusion. In this lecture we will walk through the setups of creating multiple navmeshes after a quick refresher on setting up humanoid animations with strafe sets.

Challenge Preview 00:12

Seek and Flee Preview 11:08

In this video students will follow along to implement simple seek and flee methods for a Nav Mesh agent.

Pursuit Preview 12:34

In this video students will learn about the differences in dynamics between the seek and pursuit behaviours and add an new method for moving an agent into the code.

Evade Preview 03:19

In this video students will be challenged to create their own evade method which will see the agent move away from the predicted location of the target.

Wander Preview 11:30

In this video students will examine one method of producing a wandering behaviour that will have the agent freely walk around the environment with smooth randomness.

Hide Part 1 Preview 15:21

In this video students will take a look at the basics behind the hide behaviour.  In it we will create a simple hide method that will have the agent hide behind the closest obstacle in the environment.

Hide Part 2 Preview 18:39

In this video students complete the development of the hiding behaviour and work to calculate a more accurate hiding location and reduce the number of calls to the method in the update.

Complex Behaviours Preview 10:24

In this video students will learn how to combine a number of steering behaviours with simple geometric conditions to produce complex behaviours in agents.

Behaviour Challenge Preview 08:48

In this video students will be challenged to integrate the wander behaviour into the complex behaviour to have the agents go into a wander when the target cop is out of range.

Crowd Simulation Preview 07:59

There are many factors to included in a crowd simulation... or are there? Seemingly very complex movement behaviour can be generated for groups of characters with some very simple rules. This lecture will show you how.

Creating a City Crowd Part 1 Preview 12:55

In this series of lectures we will set up a city scene with a crowd walking along the sidewalk.

Creating a City Crowd Part 2 Preview 11:20

This lecture continues on from the last adjusting animation behaviours and speeds to vary from agent to agent as well as introduces the concept of a dynamic obstacle that can move around a navmesh and be avoided by the agents.

Fleeing Preview 18:29

A behaviour seen in NPC movement is fleeing. This is a technique for steering a character away from danger. In this lecture we will look at the mathematics involved in the movement and apply it to the crowd simulation.

Flocking Part 1 Preview 20:00

Flocking is capable of producing incredible behaviour from three simple vector calculations. In this lecture you will learn how to apply this to the development of schooling fish.

Flocking Part 2 Preview 15:45

In this second part of the flocking workshop we will add extra behaviours to the fish by controlling the boundary of their swimming range.

Flocking Part 3 Preview 12:58

The third part of the flocking workshop will explore ways in which the fish can avoid obstacles in their environment through raycasting and vector calculations.

Challenge 1 Preview 00:14

Challenge 2 Preview 00:07

Challenge 3 Preview 00:08

Line of Sight Preview 14:10

In this lecture we will develop an NPC that can detect the presence of a player and act accordingly. We will implement step-by-step this algorithm which is one of the simplest AI techniques used.

Finite State Machines Part 1 Preview 10:52

Finite State Machines are a staple AI method used in games as they elegantly segregate the states, behaviours and logic used by NPCs. In part one we will begin looking at how the Unity animation system can be used to create such a machine.

Finite State Machines Part 2 Preview 14:49

In this second part of the FSM workshop we will add a base NPC class to enable sharing of properties between behaviour scripts and then add in the functionality to move from patrol to chase and back again.

Finite State Machines Part 3 Preview 09:36

In the final FSM lecture, steping you through the setup for a FSM, we will complete the example by adding in bullet shooting functionality and states onto the NPC tank.

Converting the FSM to Work on a Navmesh Preview 07:05

In this short lecture I'll show you how to convert the waypoint system used to move the tank around to have the same FSM work on a navmesh.

Challenge Preview 00:05

Introduction to Behaviour Trees Preview 03:54

In this lecture Behaviour Trees will be introduced. Well take a look at their structure and the theory of their execution.

Sequence Nodes Part 1 Preview 17:17

This lecture begins by taking you through the setup for our behaviour tree project before starting with a very simple sequencial behaviour tree as a demo.

Sequence Nodes Part 2 Preview 16:06

We continue our examination of sequences in this lecture by creating a longer set of actions and modifying different behaviour tree specific returns discover what makes sequences run and fail. Towards the end we will start creating a new behaviour tree for attacking and take a look at the wait leaf node.

Embedding Logic in Behaviour Trees Preview 11:00

In this lecture we will take a look at the while decorator node that allows you to invert the value of an action and also perform logic tests in the tree at a higher level than leaf nodes.

Selector Nodes Preview 05:15

This lecture will introduce the selector node. This type of node can be used when any (but only one) of the children nodes need to run.

More Logic for Complex Behaviours Preview 08:20

In this lecture we will complete the full set of single behaviour trees that will define all behaviours of the agent by adding the abilities to flee from danger and die.

Putting Together a Complex Behaviour Tree Preview 15:26

To finish our investigation into Behaviour Trees all the smaller trees made throughout this section will be combined to inform the total behaviour of the agent droid. A final version of the project is attached as a download.

Challenge Preview 00:12

Introduction to GOAP Preview 05:22

Goal-Orientated Action Planning is an AI technique that turns Finite State Machines on their head. It decouples goals from actions and provides for a very dynamic NPC behavioural system.

Getting Started with GOAP in Unity Preview 14:48

In this lecture we will get started with GOAP by setting up a simple scene and adding an agent.

Adding Actions to GOAP Preview 11:15

In this lecture the GOAP agent will be given its first set of actions.

Adding Multiple Plans to GOAP Preview 06:51

To create more complex behaviours in an agent you can add multiple plans that it can execute when the world states are right. In this lecture we will do this by extending the baker behaviour.

Global States and Multiple Agents Preview 13:11

Even more complexity can be added when multiple inventories and agents are introduced to the system. This lecture will take you through the addition of a world inventory as well as a new agent type.

Where To Now? Preview 11:16

This link provides further information on the courses you can look at taking based on your interests and skill level.