Search Unity

2D transition problems

Discussion in 'ML-Agents' started by TheJarmanitor, Jun 19, 2020.

  1. TheJarmanitor

    TheJarmanitor

    Joined:
    Mar 18, 2018
    Posts:
    20
    I'm working on a 2D top down enemy AI. right now i'm just trying to transition the roller ball example to my project so an enemy slime moves and gets to the target. i've tried to adjust the example code to my context, but my agent won't move at all, not even in Heuristic. I have no idea where my mistakes are. Any kind of help is highly appreciated.
    Here is my Code

    Code (CSharp):
    1.  
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.MLAgents;
    5. using Unity.MLAgents.Sensors;
    6.  
    7. public class SlimeAgent : Agent
    8. {
    9.     Rigidbody2D rb;
    10.  
    11.     public Transform target;
    12.  
    13.     public float moveSpeed=10f;
    14.  
    15.     void Start()
    16.     {
    17.         rb=GetComponent<Rigidbody2D>();
    18.     }
    19.  
    20.     public override void CollectObservations(VectorSensor sensor)
    21.     {
    22.         sensor.AddObservation(target.localPosition);
    23.         sensor.AddObservation(this.transform.localPosition);
    24.     }
    25.  
    26.     public override void OnActionReceived(float[] vectorAction)
    27.     {
    28.         Vector2 movement=Vector2.zero;
    29.         movement.x=vectorAction[0];
    30.         movement.y=vectorAction[1];
    31.         rb.MovePosition(rb.position+movement*moveSpeed);
    32.     }
    33.     public override void Heuristic(float[] actionsOut)
    34.     {
    35.         actionsOut[0] = Input.GetAxisRaw("Horizontal");
    36.         actionsOut[1] = Input.GetAxisRaw("Vertical");
    37.     }
    38. }
    39.  
     
  2. celion_unity

    celion_unity

    Joined:
    Jun 12, 2019
    Posts:
    289
    Have you verified that your code is being called? You can either do this by connecting a debugger, or using statements like
    Debug.Log("calling CollectObservations")
    . If it's not being called, make sure to either call Agent.RequestDecision(), or add a DecisionRequester component to the GameObject.

    If it is being called, the next step would be to verify whether you're getting non-zero action values.
     
  3. TheJarmanitor

    TheJarmanitor

    Joined:
    Mar 18, 2018
    Posts:
    20
    i figured it out. Turns out i was missing the decision requester. Everything is going nicely now :)