how to create and visualize agent observation in unity in ml-agents? i have 2d top-down…
- User
how to create and visualize agent observation in unity in ml-agents? i have 2d top-down view arena game like brawl stars. a top-down game, the world has a strict tile structure on a grid, but players and power-ups can be placed outside of this grid. the player sees the world from above, within the radius of his player, approximately 26x17. he can see beyond the wall where his character can’t shoot because the bullet will hit the wall. but there are also characters that can throw grenades, so walls are not a problem for them. I repeat once again, the player sees everything on the screen, even behind walls, because the view is from above. players are not tied to the grid, so you need to accurately track their position. offer me an observation.
I also need to use the checkbox to see the observation visualization even outside of training. you can change anything in the code, it is not necessary to leave mine, even observations.
how i have this ShootingAgent.cs, modify it:
using UnityEngine; using Unity.MLAgents; using Unity.MLAgents.Sensors; using Unity.MLAgents.Actuators;
public class ShootingAgent : Agent { public int observationResolution = 10; // The desired low-resolution observation space size public PlayerController playerController; private Texture2D observationTexture;
public override void OnEpisodeBegin() { // Reset the player and environment for a new episode // … playerController.InitializePlayer(); }
public override void CollectObservations(VectorSensor sensor) { // Capture the game view and convert it to low-resolution observation RenderTexture renderTexture = RenderTexture.GetTemporary(observationResolution, observationResolution); playerController.RenderToTexture(renderTexture); RenderTexture.active = renderTexture;
observationTexture = new Texture2D(observationResolution, observationResolution, TextureFormat.RGB24, false); observationTexture.ReadPixels(new Rect(0, 0, observationResolution, observationResolution), 0, 0); observationTexture.Apply();
Color[] pixels = observationTexture.GetPixels(); for (int i = 0; i < pixels.Length; i++) { sensor.AddObservation(pixels[i].r); sensor.AddObservation(pixels[i].g); sensor.AddObservation(pixels[i].b); }
RenderTexture.active = null; RenderTexture.ReleaseTemporary(renderTexture);
// Add additional observations if needed (e.g., player’s position, bullets count, etc.) playerController.CollectObservations(sensor); }
public override void OnActionReceived(ActionBuffers actions) { // Get the joystick control values from the agent’s actions float horizontal = actions.ContinuousActions[0]; float vertical = actions.ContinuousActions[1]; Debug.Log(horizontal); Debug.Log(vertical);
// Use the joystick control values to control the game playerController.ControlJoystick(horizontal, vertical);
// Set a reward based on the game state // … }
public override void Heuristic(in ActionBuffers actionsOut) { // Implement the Heuristic method for manual control (useful for debugging) ActionSegment continuousActions = actionsOut.ContinuousActions; continuousActions[0] = Input.GetAxis(“Horizontal”); continuousActions[1] = Input.GetAxis(“Vertical”); } }
- Assistant
- User
Assets\ShootingAgent.cs(62,57): error CS0246: The type or namespace name 'Player' could not be found (are you missing a using directive or an assembly reference?)
Assets\ShootingAgent.cs(63,57): error CS0246: The type or namespace name 'PowerUp' could not be found (are you missing a using directive or an assembly reference?)
- Assistant
I apologize for the confusion. I did not realize you did not have
PlayerandPowerUpclasses in your project. TheCollectObjectInfo()method is just an example of how you can collect more specific information about different objects in the scene (e.g., their type, team, health, etc.). Since I do not know the structure of your project, you should replace thePlayerandPowerUpclasses with the appropriate classes from your project, or skip this step if you do not have such classes.private void CollectObjectInfo(VectorSensor sensor, GameObject gameObject) { // Depending on your GameObject, you can extract information about type, team ID, etc. // Here's an example using one-hot encoding for type info // Replace Player and PowerUp with the appropriate classes from your project AddOneHotVector(sensor, gameObject.GetComponent<Player>().playerType, numberOfPlayerTypes); AddOneHotVector(sensor, gameObject.GetComponent<PowerUp>().powerupType, numberOfPowerUpTypes); }