Instructions to interact with a Unity environment with ML agents using Python Low-level API
- Go to Unity Hub and create a new 3D project
- In Unity, go to
Window
, then choosePackage Manager
. At the top you can seePackages: In Project
, click on it and choosePackages: Unity Registry
. Now look forML Agents
. Install the package.

- close Unity and re-open it again.
- Create a suitable environment. (We use the environment below) :

- Create a new c# script, at its header add
using Unity.MLAgents ;
, and change theMonoBehaviour
toAgent
.

- Add the appropriate logic you want. In our case, we aim to train our agent (the cube) to reach a goal (the sphere). Our code is as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.MLAgents ;
using Unity.MLAgents.Sensors;
using Unity.MLAgents.Actuators;
public class AgentScript : Agent
{
[SerializeField] private Transform targetTransform ;
public override void CollectObservations(VectorSensor sensor)
{
sensor.AddObservation(transform.localPosition);
sensor.AddObservation(targetTransform.localPosition);
}
public override void OnActionReceived(ActionBuffers actions)
{
float moveX = actions.ContinuousActions[0];
float moveZ = actions.ContinuousActions[1];
float moveSpeed = 7f;
transform.localPosition += new Vector3(moveX,0,moveZ)*Time.deltaTime*moveSpeed ;
}
public override void OnEpisodeBegin()
{
transform.position = new Vector3 (0f,1f,0f);
}
private void OnTriggerEnter(Collider other) {
if (other.tag == "Wall")
{
Debug.Log("Collision with the Wall");
SetReward(-1f);
EndEpisode();
}
if (other.tag == "Goal")
{
Debug.Log("Collision with the goal");
SetReward(1f);
EndEpisode();
}
}
}
-
Select the object to which you assigned the agent script. In the inspector window set the parameters for the Behavior Parameters :
-
Add a Decision Requester
-
For now, we only have numerical observations. In some scenarios, we may be interested in having visual observations, i.e. to use an architecture with CNNs. In the inspector of your agent, go to Add Component, and look for Camera Sensor.

- Set your camera in a suitable location to capture relevant observations that may help your model:

- In the Hierarchy panel, duplicate the camera, and change its name (we changed it to SensorCamera.It's advisable to disable the Audio Listener in the inspector of SensorCamera. Then add the duplicated camera to the Camera field in the Camera Sensor field in the inspector of the agent:

- Now let's export the environment so that we can interact with it using Python. Go to
File
, and chooseBuild Settings
. In the window that pops up, set theTarget Platform
and theArchitecture
. Don't forget to check theDevelopment Build
. Then click onBuild
at the bottom and choose a folder.
