Software and platforms
Machine learning for Unity
Unity developers use machine learning to train non-player characters, test levels with automated players and control simulated robots. With ML-Agents you define observations, actions and rewards in a scene, train a policy in Python with reinforcement or imitation learning, and run it back inside the game.
How machine learning is used in Unity
Training game characters with reinforcement learning
An agent observes the scene, picks actions and receives rewards, and a policy network learns behaviour that earns more reward over many episodes. ML-Agents connects Unity scenes to Python trainers such as PPO and SAC.
Real exampleUnity ML-Agents Toolkit paperThe paper examines Unity and the open-source ML-Agents Toolkit as a general platform for building learning environments and training agents.
Automated playtesting
Agents trained to explore or exploit a game can reach more of a level than scripted tests and find exploits, stuck spots and difficulty problems. Designers then review what the agents found.
Real exampleAugmenting Automated Game Testing with Deep Reinforcement LearningThis paper shows deep reinforcement learning increasing test coverage, finding exploits and testing map difficulty in game testing.
Synthetic training data for computer vision
A scene can render thousands of labelled images with randomised lighting, poses and backgrounds, which is useful when real labelled photos are scarce. The labels such as bounding boxes come from the engine, so they are exact.
Real exampleUnity Perception packageThe Unity Perception paper introduces an open-source package for generating annotated synthetic datasets and reports a 2D object detector trained mostly on synthetic data.
Simulating and training robots
Robots can be modelled in a Unity scene and connected to robotics software, so control and perception models are trained and tested before they touch hardware.
Real exampleUnity Robotics HubUnity's Robotics Hub collects the tools, tutorials and documentation for robotics simulation in Unity.
Can you learn machine learning for Unity?
Yes. Unity developers can train a first agent from the ML-Agents examples within a day. To design your own agents, add Python, the basics of neural networks and the reinforcement learning loop of states, actions, rewards and episodes.
- Python for training runsML-Agents trains in Python, so learn enough to manage an environment, edit a YAML configuration and read training logs.Learn it in Python
- Machine learning basicsLearn what a model, loss and evaluation are, and why results must be checked on episodes or seeds the model did not train on.Learn it in Machine Learning
- Neural networksPolicies in ML-Agents are neural networks trained with PyTorch, so learn layers, backpropagation and optimisers.Learn it in Deep Learning
- Reinforcement learning and reward designLearn how PPO and SAC update a policy, then practise writing rewards that cannot be gamed in your own scenes.
Moving into machine learning for games and simulation
If you already work in the field
As a Unity developer you already build scenes, physics and C# logic, which is most of what an ML-Agents environment needs. Add Python and reinforcement learning basics, then train an example environment before designing rewards for your own game.
If you come from data science or ML
If you know reinforcement learning in Python, learn Unity's editor, C# scripting and how ML-Agents defines observations, actions and rewards. Rebuild one environment you know, such as a balancing task, to learn the workflow.
Roles that do this workGameplay AI programmerMachine learning engineer, gamesSimulation engineerRobotics simulation engineerTechnical designer
Tools you will use
| Tool | What you use it for |
|---|---|
| Unity ML-Agents | Connect Unity scenes to Python trainers for PPO, SAC and imitation learning of agent behaviour. |
| Unity Inference Engine | Run trained ONNX models inside a Unity game or app at runtime on the player's device. |
| PyTorch | The deep-learning framework ML-Agents trains with, and the place to customise network architectures. |
| TensorBoard | Watch reward, episode length and loss curves while an ML-Agents training run progresses. |
| Stable-Baselines3 | Compare reference reinforcement-learning algorithms on Unity environments wrapped as Gym environments. |
Public data to practise on
- ML-Agents example environments: Ready-made Unity scenes with defined observations, actions and rewards; train and compare agents on each task.
Your first project
Open the 3DBall scene from the ML-Agents example environments, train a PPO agent with the default configuration, and compare it with a random-action baseline using mean episode reward over new random seeds.
Common questions
Is Unity ML-Agents free?
Yes. ML-Agents is open source, and the Python trainers and example environments are available on GitHub.
Do I need to know Python to use ML-Agents?
You need a little. Agents are written in C# inside Unity, but training runs from Python, and changing the network or algorithm means editing Python code.
Can a trained agent run in the final game?
Yes. ML-Agents exports trained policies as ONNX models, which run inside Unity at runtime on the player's device.
Why does my ML-Agents agent learn strange behaviour?
Usually the reward allows a shortcut, such as earning points without finishing the task. Watch episodes, shape the reward carefully and check that observations contain what the agent needs.
Courses for these skills
- Deep Learning with PyTorchBuild neural policy and optimisation foundations.Syllabus available · lessons open as produced
- Python for AI and Machine LearningUnderstand the programming and data structures used around an experiment.33 published lessons · all planned lessons open
- Maths for Machine LearningConnect objectives, state variables and optimisation.26 published lessons · all planned lessons open
New to programming or maths? Start with Python, or follow the machine learning path in order. Published lessons are free to read; compare Free and Pro.