A Unity-based racing game featuring an AI agent trained using Reinforcement Learning (Unity ML-Agents).
This project demonstrates a racing game where an AI driver learns to navigate the track autonomously. The AI has been trained using Unity ML-Agents, allowing it to optimize its driving path and speed through reinforcement learning algorithms.
- Assets/: Contains the Unity project source code, scenes, scripts, and game assets.
- results/: Stores the training results, including the neural network models (
.onnxfiles) and TensorBoard logs generated by ML-Agents. - venv/: Python virtual environment containing the necessary dependencies for training the agent.
- ML-Demos/: (Optional) Additional demos or configurations related to the ML environment.
- Unity Hub
- Unity Editor (Recommended: 2021.3 LTS or newer)
- Python 3.7+ (for training the AI)
-
Clone the repository:
git clone [https://github.com/gabinollier/RacingGame.git](https://github.com/gabinollier/RacingGame.git) cd RacingGame -
Open the project in Unity:
- Launch Unity Hub.
- Click "Open" and select the
RacingGamefolder. - Wait for Unity to import the assets.
-
Setup Python Environment (for training):
- Ideally, create a new virtual environment to avoid conflicts, or use the provided
venv. - Install the dependencies:
pip install mlagents
- Ideally, create a new virtual environment to avoid conflicts, or use the provided
- Open the main scene located in
Assets/Scenes/. - Press the Play button in the Unity Editor.
- Watch the AI drive (or control the car using the PlayerController).
If you wish to retrain the agent or improve its performance:
-
Open your terminal/command prompt and activate the Python virtual environment.
-
Navigate to the project root.
-
Run the ML-Agents training command:
mlagents-learn config/trainer_config.yaml --run-id=RacingAI_01
-
Press Play in the Unity Editor when prompted by the terminal.
The AI uses a Reinforcement Learning approach where:
- Observations: Raycasts (lidar-like vision), speed, and position.
- Actions: Steering, acceleration, and braking.
- Rewards: Positive rewards for moving forward/staying on track, negative rewards for hitting walls or going off-track.