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
- User
You are using an old version of ML Agents. Use the new version where the imports start with: using UnityEngine; using Unity.MLAgents; using Unity.MLAgents.Sensors; using Unity.MLAgents.Actuators;
I understood about food, I only need a new code on the new version of ML Agents for our CircleAgent.cs
- Assistant
Apologies for that. Here's the updated code using the latest version of Unity ML Agents:
- Make sure to import the following namespaces in "CircleAgent.cs":
using UnityEngine; using Unity.MLAgents; using Unity.MLAgents.Sensors; using Unity.MLAgents.Actuators;- Update the CircleAgent class as follows:
public class CircleAgent : Agent { public GameObject foodPrefab; public float moveSpeed; private Rigidbody2D rb; public override void Initialize() { rb = GetComponent<Rigidbody2D>(); } public override void OnEpisodeBegin() { transform.localPosition = new Vector2(Random.Range(-8f, 8f), Random.Range(-4.5f, 4.5f)); GameObject.FindWithTag("Food").transform.localPosition = new Vector2(Random.Range(-8f, 8f), Random.Range(-4.5f, 4.5f)); } public override void CollectObservations(VectorSensor sensor) { sensor.AddObservation(transform.localPosition); sensor.AddObservation(GameObject.FindWithTag("Food").transform.localPosition); } public override void OnActionReceived(ActionBuffers actions) { Vector2 direction = new Vector2(actions.ContinuousActions[0], actions.ContinuousActions[1]); rb.velocity = direction.normalized * moveSpeed; } public override void Heuristic(in ActionBuffers actionsOut) { ActionSegment<float> continuousActions = actionsOut.ContinuousActions; continuousActions[0] = Input.GetKey(KeyCode.W) ? 1 : (Input.GetKey(KeyCode.S) ? -1 : 0); continuousActions[1] = Input.GetKey(KeyCode.D) ? 1 : (Input.GetKey(KeyCode.A) ? -1 : 0); } private void OnTriggerEnter2D(Collider2D collision) { if (collision.CompareTag("Food")) { AddReward(1.0f); EndEpisode(); } } }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.