SlideShare a Scribd company logo
Machine Learning in Unity - How to give your game AI a real brain
2
Привет!
My name is Ciro Continisio
Technical Evangelist at Unity
3
Привет!
My name is Alessia Nigretti
Technical Evangelist at Unity
4
Machine
Learning in Unity
5
Introduction
What is Machine Learning?
6
What is Machine Learning
7
What is Machine Learning
Reinforcement Learning
8
A computer system
modelled on
the human brain and
nervous system
Neural Networks
What is Machine Learning
9
Machine Learning
Agents
10
Reinforcement Learning
11
Learning Environments
12
Learning Environments
13
Learning Environments
14
Example Projects
15
3D Ball
Goal:
Balance the ball on the platform
Reward:
● +0.1 for every frame the ball
remains on the platform
● -1.0 if the ball falls from the
platform
16
Propellers
Goal:
Have the cubes learn to float
Reward:
● +0.1 for each frame the cube floats
● -1.0 for each collision with the floor
17
Arena
Goal:
Push the crate out of the arena
Rewards:
● +0.2 for if closing on the crate
● +0.5 when crates gets further from
the center
● Neg. rewards for delaying, or falling
18
Bounce Ball
Goal:
Bounce ball on top of agent’s head
Reward:
● +0.1 for each frame the ball is
closer to the agent
● -0.1 for each frame the ball is
further away from the agent
19
Problem
Can you use Machine Learning
in a real game?
20
Roguelike
21
Roguelike Game
Ingredients
• A simple action game
• All entities are Agents, both the player and the enemies
• Establish a common “interaction language”
• The goal is survival, while attacking other entities
22
Setting up the training
23
Setting up the training
Design and
ideas
• What are the game actions
• What you want the Agent
to learn
• What’s right or wrong
(what to reward)
24
Discrete vs. Continuous
Discrete means that the States/Actions can only have one value
at a time. Like an Enum. It’s either 0, or 1, or 2, or 3, etc.
⬝ Easier: Agents associate actions with rewards more easily
In Roguelike, we use Discrete for Actions. It can have 6 values:
0: Stay still / 1-4: Move in one direction / 5: Attack
Setting up the training
25
Discrete vs. Continuous
Continuous means you can have multiple States (or Actions)
and they all have float values.
⬝ They require more memory for training (hyperparameters)
⬝ Hard to use: they can confuse the Agent
In Roguelike, we use Continuous for States:
health, canAttack, hasTarget, distanceFromTarget, …
Setting up the training
26
Setting up the training
The pseudo-algorithm (AgentStep)
If health > 50% then
If current distance < previous distance then
Reward
End
Else
If current distance > previous distance then
Reward
End
End
If input is attack
If can attack then
Start attack
Else
Punish
Else
If is not healing and health < max health then
Start healing
End
End
Movement Attack
27
Spoiler
This initial algorithm has changed
a lot
28
Tips on rewards
• Rewards can come in the AgentStep function, but also at
other times (OnCollisionEnter, etc.)
• Agents will find a way to exploit the rewards!
• Small details in rewards influence the learning process
Setting up the training
reward = .2f / (distanceSqr + .01f); > reward = .2f;
29
Training scene
• Position and configure the
agent(s)
• Connect them to the
relevant Brains
• Configure the Academy
Setting up the training
30
Demo time!
31
Tips for the training environment
• Different situations in parallel help Agents to learn better
• Heuristic Agents are the perfect training dummies!
• Before launching a 1 hour training:
• Double-check your logic so you don’t make wrong
assumptions
• Launch a 1x speed training to see what’s happening
Setting up the training
32
Building and training
• Set the Brain to External
• Build!
• Set up python environment and hyperparameters
• Launch training
Training
33
Demo time!
34
Training with Tensorflow
• Observe the mean reward
• Stop when it looks stable
• Export the model, import into Unity
• Set the Brain to Internal
• Play!
Training
35
Demo time!
36
Final tips and
Key takeaways
37
Tips on hyperparameters
• Beta: is the randomisation of actions. If agents corner
themselves on a behaviour quickly, increase beta
• Batch size, Buffer size, Hidden units: they differ a lot
between using Discrete or Continuous spaces
Read the guide: github.com/Unity-Technologies/ml-agents
Training
38
Tips and takeaways
Physics
Because ML runs on the FixedUpdate (for stability):
• Remember Rigidbody.position doesn’t change mid-frame
• Switch Animators from Normal to Animate Physics if
animation is key in the training
• If using interpolation on the RB, Rigidbody.position or
Rigidbody.MovePosition( ) behave differently
39
Tips and takeaways
Build tools
The training process can be
long and repetitive.
Make your life easier by
building some little tools.
40
One last demo!
41
Next
What now?
• Mix Trained AI with Heuristic AI to obtain final behaviour
Learning from the player
• Gather players’ behaviour and train agents (offline)
based on the information you obtained
• Coming soon: Imitation Learning!
42
Спасибо!
Ciro Continisio
ciro@unity3d.com
@CiroContns
Alessia Nigretti
alessian@unity3d.com
@AlessiaNigretti
Get the demo and presentation:
bit.ly/UnityDevgammMinsk

More Related Content

PPTX
Ciro Continisio - Implementing Machine Learning the Unity way - Codemotion Mi...
PPTX
Reinforcement learning
PPTX
REAL Training Presentation
PPTX
Reinforcement Learning – a Rewards Based Approach to Machine Learning - Marko...
PDF
Reinforcement Learning using OpenAI Gym
PPTX
Machine learning module_1_ppt vtu...pptx
PDF
Reinforcement learning
PDF
An introduction to reinforcement learning
Ciro Continisio - Implementing Machine Learning the Unity way - Codemotion Mi...
Reinforcement learning
REAL Training Presentation
Reinforcement Learning – a Rewards Based Approach to Machine Learning - Marko...
Reinforcement Learning using OpenAI Gym
Machine learning module_1_ppt vtu...pptx
Reinforcement learning
An introduction to reinforcement learning

Similar to Machine Learning in Unity - How to give your game AI a real brain (20)

PPTX
Online learning &amp; adaptive game playing
PDF
【Unite Tokyo 2018】Unity for ディープ・ラーニング:ツールキット『ML-Agents』のご紹介
DOCX
How to Allow Java Applets for physics labsWindows 7Ope.docx
PPTX
OpenAI Gym & Universe
PDF
Reinforcement Learning
PDF
Creative Engineering 101
PPTX
Intro to Reinforcement Learning
PDF
Design Thinking + Lean + Agile
PDF
Getting better
PDF
Designing Games for Industrial Training.
PDF
Leap from Doing Agile to Being Agile_AAC2019
PDF
Behavioral hypothesis of team behavior
PDF
BA and Beyond 19 - Pieter Van Driessche - The 7 hidden layers behind agile te...
PPTX
24.09.2021 Reinforcement Learning Algorithms.pptx
PPTX
Genetic Algorithms for Evolving Computer Chess Programs
PPTX
Will Robots Replace Testers?
PDF
Game theory for a better world
PPTX
Building a deep learning ai.pptx
PDF
Purple Team Exercise Workshop December 2020
PDF
Briefly About Reinforcement Learning which we are using in our Esports project?
Online learning &amp; adaptive game playing
【Unite Tokyo 2018】Unity for ディープ・ラーニング:ツールキット『ML-Agents』のご紹介
How to Allow Java Applets for physics labsWindows 7Ope.docx
OpenAI Gym & Universe
Reinforcement Learning
Creative Engineering 101
Intro to Reinforcement Learning
Design Thinking + Lean + Agile
Getting better
Designing Games for Industrial Training.
Leap from Doing Agile to Being Agile_AAC2019
Behavioral hypothesis of team behavior
BA and Beyond 19 - Pieter Van Driessche - The 7 hidden layers behind agile te...
24.09.2021 Reinforcement Learning Algorithms.pptx
Genetic Algorithms for Evolving Computer Chess Programs
Will Robots Replace Testers?
Game theory for a better world
Building a deep learning ai.pptx
Purple Team Exercise Workshop December 2020
Briefly About Reinforcement Learning which we are using in our Esports project?
Ad

More from DevGAMM Conference (20)

PPTX
The art of small steps, or how to make sound for games in conditions of war /...
PPTX
Breaking up with FMOD - Why we ended things and embraced Metasounds / Daniel ...
PPTX
How Audio Objects Improve Spatial Accuracy / Mads Maretty Sønderup (Audiokine...
PPTX
Why indie developers should consider hyper-casual right now / Igor Gurenyov (...
PPTX
AI / ML for Indies / Tyler Coleman (Retora Games)
PDF
Agility is the Key: Power Up Your GameDev Project Management with Agile Pract...
PPTX
New PR Tech and AI Tools for 2023: A Game Changer for Outreach / Kirill Perev...
PDF
Playable Ads - Revolutionizing mobile games advertising / Jakub Kukuryk (Popc...
PDF
Creative Collaboration: Managing an Art Team / Nastassia Radzivonava (Glera G...
PDF
From Local to Global: Unleashing the Power of Payments / Jan Kuhlmannn (Xsolla)
PDF
Strategies and case studies to grow LTV in 2023 / Julia Iljuk (Balancy)
PDF
Why is ASO not working in 2023 and how to change it? / Olena Vedmedenko (Keya...
PDF
How to increase wishlists & game sales from China? Growth marketing tactics &...
PDF
Turkish Gaming Industry and HR Insights / Mustafa Mert EFE (Zindhu)
PDF
Building an Awesome Creative Team from Scratch, Capable of Scaling Up / Sasha...
PPTX
Seven Reasons Why Your LiveOps Is Not Performing / Alexander Devyaterikov (Be...
PDF
The Power of Game and Music Collaborations: Reaching and Engaging the Masses ...
PPTX
Branded Content: How to overcome players' immunity to advertising / Alex Brod...
PPTX
Resurrecting Chasm: The Rift - A Source-less Remastering Journey / Gennadii P...
PPTX
How NOT to do showcase events: Behind the scenes of Midnight Show / Andrew Ko...
The art of small steps, or how to make sound for games in conditions of war /...
Breaking up with FMOD - Why we ended things and embraced Metasounds / Daniel ...
How Audio Objects Improve Spatial Accuracy / Mads Maretty Sønderup (Audiokine...
Why indie developers should consider hyper-casual right now / Igor Gurenyov (...
AI / ML for Indies / Tyler Coleman (Retora Games)
Agility is the Key: Power Up Your GameDev Project Management with Agile Pract...
New PR Tech and AI Tools for 2023: A Game Changer for Outreach / Kirill Perev...
Playable Ads - Revolutionizing mobile games advertising / Jakub Kukuryk (Popc...
Creative Collaboration: Managing an Art Team / Nastassia Radzivonava (Glera G...
From Local to Global: Unleashing the Power of Payments / Jan Kuhlmannn (Xsolla)
Strategies and case studies to grow LTV in 2023 / Julia Iljuk (Balancy)
Why is ASO not working in 2023 and how to change it? / Olena Vedmedenko (Keya...
How to increase wishlists & game sales from China? Growth marketing tactics &...
Turkish Gaming Industry and HR Insights / Mustafa Mert EFE (Zindhu)
Building an Awesome Creative Team from Scratch, Capable of Scaling Up / Sasha...
Seven Reasons Why Your LiveOps Is Not Performing / Alexander Devyaterikov (Be...
The Power of Game and Music Collaborations: Reaching and Engaging the Masses ...
Branded Content: How to overcome players' immunity to advertising / Alex Brod...
Resurrecting Chasm: The Rift - A Source-less Remastering Journey / Gennadii P...
How NOT to do showcase events: Behind the scenes of Midnight Show / Andrew Ko...
Ad

Recently uploaded (20)

PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PPTX
Transform Your Business with a Software ERP System
PDF
System and Network Administration Chapter 2
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PPTX
Essential Infomation Tech presentation.pptx
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
ai tools demonstartion for schools and inter college
PDF
Nekopoi APK 2025 free lastest update
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
AI in Product Development-omnex systems
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Which alternative to Crystal Reports is best for small or large businesses.pdf
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Transform Your Business with a Software ERP System
System and Network Administration Chapter 2
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Wondershare Filmora 15 Crack With Activation Key [2025
How Creative Agencies Leverage Project Management Software.pdf
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
2025 Textile ERP Trends: SAP, Odoo & Oracle
Essential Infomation Tech presentation.pptx
VVF-Customer-Presentation2025-Ver1.9.pptx
Design an Analysis of Algorithms I-SECS-1021-03
ai tools demonstartion for schools and inter college
Nekopoi APK 2025 free lastest update
Softaken Excel to vCard Converter Software.pdf
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
AI in Product Development-omnex systems
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free

Machine Learning in Unity - How to give your game AI a real brain