Create me a simple Unity game in 2D with a top view. The circle character should WASD in…
- User
Create me a simple Unity game in 2D with a top view. The circle character should WASD in all directions, and food circles will spawn around him. The most important condition is to add ML Agents and then explain to me how to set everything up. If anything, I have Unity and ML Agents installed and imported. I only need the code and instructions on how to make the training of the neural network start, as well as how to switch to keyboard control. The code must be complete and working.
- Assistant
To create a simple Unity game with ML agents, follow these steps:
- Create a new Unity project with 2D settings.
- Create a new folder called "Scripts" in the Assets folder.
- Download and import "ml-agents" and "ml-agents.extensions" Unity packages in the project: https://github.com/Unity-Technologies/ml-agents/releases
- Inside the "Scripts" folder, create a new "C#" script called "PlayerAgent".
- Replace the content of the "PlayerAgent" script with the following code:
using System.Collections; using System.Collections.Generic; using UnityEngine; using Unity.MLAgents; using Unity.MLAgents.Sensors; public class PlayerAgent : Agent { public float speed = 5.0f; public Transform target; private Rigidbody2D rb; private void Start() { rb = GetComponent<Rigidbody2D>(); } public override void OnActionReceived(float[] vectorAction) { Vector2 movement = new Vector2(vectorAction[0], vectorAction[1]).normalized; float distanceToTarget = Vector2.Distance(transform.position, target.position); if (distanceToTarget < 1.0f) { AddReward(1.0f); EndEpisode(); } AddReward(-0.001f * distanceToTarget); rb.velocity = movement * speed; } public override void Heuristic(float[] actionsOut) { // Input from keyboard actionsOut[0] = Input.GetAxisRaw("Horizontal"); actionsOut[1] = Input.GetAxisRaw("Vertical"); } public override void OnEpisodeBegin() { transform.position = new Vector2(Random.Range(-8f, 8f), Random.Range(-8f, 8f)); target.position = new Vector2(Random.Range(-8f, 8f), Random.Range(-8f, 8f)); } public override void CollectObservations(VectorSensor sensor) { sensor.AddObservation(transform.position); sensor.AddObservation(target.position); sensor.AddObservation(rb.velocity); } }- In the Hierarchy, create a new GameObject called "Game" and add the "PlayerAgent" script to it. Create another GameObject called "Target". Assign the Target object to the "Target" field in the "Game" GameObject.
- Add a Rigidbody2D component to the "Game" GameObject and set its Gravity Scale to 0.
- Create a new folder called "Prefabs" in the Assets folder and drag both "Game" and "Target" objects to create prefabs.
- Remove the "Game" and "Target" objects from the Hierarchy.
- Inside the "ml-agents" folder, create an "agent.yaml" file with the following content:
behaviors: PlayerAgent: trainer_type: ppo hyperparameters: batch_size: 10 buffer_size: 100 learning_rate: 0.0003 beta: 0.005 epsilon: 0.2 lambd: 0.95 num_epoch: 3 learning_rate_schedule: linear network_settings: normalize: true hidden_units: 512 num_layers: 2 vis_encode_type: simple reward_signals: extrinsic: gamma: 0.99 strength: 1.0 keep_checkpoints: 5 max_steps: 200000 time_horizon: 512 summary_freq: 5000 threaded: true- Go to Window > ML-Agents > Runtime Trainer.
- In the settings, select your "agent.yaml" file for the "Behavior Name" and check "Control".
- Click on "Start Training" to start training.
To switch between keyboard and agent control, toggle the "Control" checkbox in the Runtime Trainer window. When "Control" is checked, the agent will be controlled by the neural network, and when unchecked, you'll control the agent using the WASD keys on the keyboard.
Note: You may have to adjust some parameters (e.g., learning rate, max_steps) to get better results depending on the agent's behavior and training progress.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.