Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

Create me a simple Unity game in 2D with a top view. The circle character should WASD in…

  1. 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.

  2. Assistant

    To create a simple Unity game with ML agents, follow these steps:

    1. Create a new Unity project with 2D settings.
    2. Create a new folder called "Scripts" in the Assets folder.
    3. Download and import "ml-agents" and "ml-agents.extensions" Unity packages in the project: https://github.com/Unity-Technologies/ml-agents/releases
    4. Inside the "Scripts" folder, create a new "C#" script called "PlayerAgent".
    5. 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);
        }
    }
    
    1. 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.
    2. Add a Rigidbody2D component to the "Game" GameObject and set its Gravity Scale to 0.
    3. Create a new folder called "Prefabs" in the Assets folder and drag both "Game" and "Target" objects to create prefabs.
    4. Remove the "Game" and "Target" objects from the Hierarchy.
    5. 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
    
    1. Go to Window > ML-Agents > Runtime Trainer.
    2. In the settings, select your "agent.yaml" file for the "Behavior Name" and check "Control".
    3. 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.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.