Unity and ML-Agents
Install
Install Unity
- Ref
- Download and install Unity Hub and open it
- Go to the install tab and install Unity (preferably choose LTS version)
Install ML Agents
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
- Go to the github link
-
Download the "Verified Package 1.0.7" from the following,

Make your Environment in Unity
Show details
Open Unity Hub and click "NEW"

Select 3D and enter project name and location. Then click CREATE button.

Train
Config
Ref
Training command
After opening Unity scene, check if the behavior name is same as the name inside config ".yaml" file.
Check if Behaviour type is set to default.
Test Examples - 3D Ball
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
Setup
- Open Unity and install MLAgents from
Window -> Package Manager -> MLAgents -> Install
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
- Open 3DBall Scene from the assets folder.
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
- The screen should look like this
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
Test the pretrained model
- Click on the play button to see pretrained model's inference result balancing the ball on the cube.
Train
- Train the 3Dcube by entering the following comand in the console.
mlagents-learn config/trainer_config.yaml --run_id ANY_NAME --train
- It will create a folder named 'ANY_NAME' and store training results like tensorboard log info, weight file (.nn) in this folder.
Test your trained model
- To test the training output, drag the .nn file to Model input in Behavior Parameters component of Agent in Unity.
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
- Finally click on play button to check the inference result.
BallJump
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
Objective
Move Ball to the target box in 3D space.
Setup
Install MLAgents and ProBuilder package into Unity from Window -> Package Manager
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
Unity Environment
Your environment should include an agent with components:
- Collider component (box, shpere or mesh)
- Rigid body: To get affected by forces of the environment
- Decision Requester component
- Agent Logic Script component (Behaviour Parameters Component is added automatically with this component)
The screenshot of the BallJump environment I created is shown below.
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
Agent Logic Script
- The complete script of Agent Logic is here.
BallAgentLogic.cs
- We need to import this 3 classes,
- Make a custom class by inheriting Agents class from MLAgents.
- Overwrite the following 3 methods from the Agents class
- OnEpisodeBegin
- CollectObservations
- OnActionReceived
- Heuristic (Not necessary to write): This function allows us to control the agents using keyboard or any way other than Neural Network.
Observations (9)
- Target's location (3 observation)
- Agent's location (3 observation)
- Agent's velocity (3 observation)
Actions (2; discrete)
- move vector x (+1 = right, -1 = left)
- move vector z (+1 = forward, -1 = backward)
Rewards
- Agent touches the target: +1
Training
Config parameters
Shell command
mlagents-learn PATH_TO_CONFIG --run-id RUN_ID --train
Tensorboard result
See Graphs



Humingbird
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
Project Setup
- Download
- Create new project in Unity Hub by selecting "Universal Render Pipeline"
- Drag the downloaded Humingbird scene to Asset folder inside Unity
- Double click the Training file inside the folder Humingbird/Scenes
- (Optional) Delete the folders "Example", "Scenes", "Tutorial" and a file named "Readme" inside the Assets folder. (Don't delete anything inside Hummingbird folder)
- Go to "edit/Project Settings/Physics", change Default Contact Offset from 0.01 to 0.001 so that the collision works better in this project.
- Fix lighting by going to
- "Windows/Rendering/Lighting Settigs", click "Generate Lighting" button.
- Assets/Materials, drag Skybox_Mat to "Skybox Material" inside Lighting/Environment.
C# scripts
- Reset flower
- Set nectar-extract speed
- Turns from red to purple after nectar is extracted
- Search flower_plant tag
- Get flowerNector
- Find the nearest flower
- Calculate the distance between Bird's beak-tip and the nearest flower
- Bird move to safe random position when episode starts
- freeze the bird
- Triggers if bird enter nectar area
- Heuristic: keybord controls
RL
Observations (10)
- Bird's rotation (4 observations)
- Distance between Bird's beak-tip and the nearest flower (3 observation)
- Check if Bird's beak-tip is in front of flower or not (-1 to 1)
- Check if Bird's beak-tip is pointing towards the flower or not (-1 to 1)
- Relative distance from the Bird's beak-tip and the nearest flower to the flower area diameter (0 to 1)
Actions (5; continuous)
- move vector x (+1 = right, -1 = left)
- move vector y (+1 = up, -1 = down)
- move vector z (+1 = forward, -1 = backward)
- pitch angle (+1 = pitch up, -1 = pitch down)
- yaw angle (+1 = turn right, -1 = turn left)
Rewards
- Inside nectar area: 0.1
- Inside nectar area and drinking it: 0.2
- Colliding boundary: -0.5
Scene Setup
Load FloatingIsland from Prefabs
- Create 3 tags in Project Settings, then
- Add boundary tag in the IslandBoundaries
- Add nectar tag in the FlowerNectarCollider
- Add flower_plant tag in the FlowerPlant
- Add script components
- Add FlowerArea script as a component to FloatingIsland
- Add Flower script as a component to Flower inside FlowerBud
- Add Hummingbird script as a component to Flower inside FlowerBud
- Add DecisionRequester component; 5 steps
- HummingbirdAgent script and Behaviour component
- Ray perseption
- Heuristic
Tensorboard result
3D Ball

Observations (8)
- rotation.z
- rotation.x
- ball.transform.position - gameObject.transform.position
- ball.velocity
Actions (2; continuous)
- rotate along z axis
- rotate along x axis
Rewards
- -1
- |ball.position.y - agent.position.y|
- |ball.position.y - agent.position.y|
- |ball.position.y - agent.position.y|
- 0.1
Basic

Observations (3)
- Agent position
- small goal position
- big goal position
Actions (1; discrete)
- 1 axis direction
Rewards
- -0.01: no goal
- 0.1: small goal
- 1: big goal
Bouncer

Observations (6)
- Agent position (3 observation)
- target position (3 observation)
Actions (3; continuous)
- x axis force
- y axis force (scale from 0 to 1)
- z axis force
Rewards
- -0.05 x (mean square of actions)
- -1: Agent.position.y < -1
- -1: outside the floor
- +1: Touch the target
Crawler
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
Observations (126)
- For each body part (bp)
- is touching the ground?
- velocityRelativeToLookRotationToTarget
- angularVelocityRelativeToLookRotationToTarget
- For each body part except center-body
- localPosRelToBody
- bp.currentXNormalizedRot
- bp.currentYNormalizedRot
- bp.currentZNormalizedRot
- strength ratio (current strength/ max strength)
Actions (20; continuous)
Rewards
ToDo
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ



Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
Observations (6)
Actions (3; continuous)
Rewards