Unity ML-Agents Tutorials – Complete Guide

Unity ML-Agents, is an open source toolkit developed by Unity to enhance a game’s AI with machine learning. Typically when developing an AI for a game, you’d check to see if a certain condition is true (i.e. can you see the player?) and then execute a certain action (i.e. attack). This form of AI works, but at the core of things it can be predictable and limiting.

Machine learning allows agents (enemy, AI car, anything you want to have an AI) to automatically learn through reinforcement learning, imitation learning and many other learning types. What this means, is that you’re not specifically telling the agent what to do. Instead, you’re developing their brain overtime in order for them to determine how to go about a certain task with a number of given inputs.

Let’s Look at an Example

Let’s go over an example of training an ML agent (this is from a Unity ML-Agents sample project). We have an agent who can move and turn on a flat surface. Their objective is to push a block into the end goal. We can train this agent’s brain, so that no matter the starting position of the block or goal, it will always be able to complete it. The agent can also detect the surrounding world with 14 raycasts shooting out from all directions. This can give info about what the agent can see and how far away.

Most likely, the first session will start with the agent standing still or moving around in a random direction. If we continue to run these simulations many, many times, eventually the agent will hit the block and maybe even accidentally move it into the goal. This is where agent rewards come in handy. Whenever the agent does something that progresses its learning (i.e. moving the block into the goal), we give it +1 reward. In general, this means the rewarded behaviour will carry over to future simulations and overtime the agent will gather more knowledge on where to stand relative to the block, the direction it needs to push, etc. After hundreds (maybe even thousands) of simulations the agent’s brain should be developed enough so that it can push the block (no matter the starting position) into the goal every time.

Here’s a look at the push block example we just went over.

ML-Agents Package

Right now, ML-Agents is still in development but it can be downloaded from the Unity GitHub page here. Furthermore, since it’s still in development and since the training is done through Python, there are additional things you need to download and setup but the provided tutorials will go through that. ML-Agents also includes 15+ example environments, showing many different game types and how those are trained.

Resource Links



Other New Unity Features

Don't miss out! Offer ends in
  • Access all 200+ courses
  • New courses added monthly
  • Cancel anytime
  • Certificates of completion