- C#
- Python
- ML-Agents
You'll need to install the mlagents python library:
- Install conda: https://docs.conda.io/projects/conda/en/latest/user-guide/install/index.html. Conda allows you to manage multiple python versions and Python virtual environments (note: not virtual machines) on a single machine.
- Create a virtual environment for mlagents, then activate it. Run,
conda create -n mlagents python=3.10.12 && conda activate mlagents
If you restart your terminal, you’ll need to re-run theconda activate mlagents
command. (Make sure to run the rest of these commands in that same terminal session, where the conda environment is activated) - (windows only) install pytorch into your virtual env: pip3 install torch~=2.2.1 --index-url https://download.pytorch.org/whl/cu121
- Install the mlagents python package: python -m pip install mlagents==1.1.0
Do this to update the models for the "easy", "medium", and "hard" difficulty levels.
- Start the conda environment:
conda activate mlagents
- Enter the Assets directory:
cd Assets/
- Start this script:
python3 train_models.py
- When the script outputs
[INFO] Listening on port 5004. Start training by pressing the Play button in the Unity Editor.
Go to the "18PlayerAITrainer" scene in Unity, and press play. - (wait for the script to finish, about 10 minutes)
When the script is done, it should have created new model snapshot (.onnx) files in the Assets/agent-models directory. These files are the trained models for the 3 AI difficulties and will automatically be loaded by the Unity game the next time you run or built it.
If you want more control over the mlagents-learn options, you can invoke it directly using the following steps:
- Start the conda environment:
conda activate mlagents
- Enter the Assets directory:
cd Assets/
- Run
mlagents-learn agent-training-configs/config_v0.yaml --run-id=my_run_id --train
Changemy_run_id
to a name for your model. That script should run, then get to a point where it says it's waiting for Unity. - In Unity, open up the
18PlayerAITrainer
scene and press Play - (At this point, the ML Agents script should take control of the game and start playing through a bunch of training episodes. This can take about 10 minutes to run)
When that's finished, there should be some new files in the Assets/results/ directory for your model. In the next step, we'll tell the game to use that new model.
- Open up the
MainMenu
Scene. - Open
MainManager
in the inspector. - Set one of the
Difficulty Models
to thePaddle.onnx
file your training just created.
To test out your new model, the easiest way is:
- Open up the
MainMenu
Scene and press Play - Select the difficulty level your model is assigned to
- Select the "AI Player" mode, then watch your model in action!
Here are some handy tips for solving common problems:
- If the Unity editor crashes while you're trying to play a scene that includes an agent running in inference mode, check that the model you're using was trained using the same Vector Observation Stack Size as what's currently being used in your code. If there's a mismatch, that'll likely result in a crash (based on my experience)