0

I am trying to make an AI using ML agents but I can't seem to get the camera sensor to actually give input. I have followed several tutorials but none of them go in depth enough for what I want and if they do they are very outdated. Here is my code:

    using UnityEngine;
    using Unity.MLAgents;
    using Unity.MLAgents.Sensors;
    using UnityEngine.SceneManagement;
    using Unity.MLAgents.Actuators;

    public class MyAgent : Agent
    {
    Rigidbody2D rb2D;
    public bool space;
    public Vector3 targetPos;
    [SerializeField] CameraSensor Observer;
    public void Start()
    {
        rb2D = GetComponent<Rigidbody2D>();
        Observer = GetComponent<CameraSensor>();
    }
    public override void OnActionReceived(ActionBuffers actions)
    {
        if (actions.DiscreteActions[1] == 1)
        {
            space = true;
            RaycastHit2D hit = Physics2D.Raycast(transform.position, new Vector2((float)0.1, 1), 100000.0f, 1);
            targetPos = hit.point;
        }
        else
        {
            space = false;
        }
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "Obstacles")
        {
            AddReward(-50f);
            EndEpisode();
            SceneManager.LoadScene(0);
        }
        else if (collision.tag == "Checkpoint")
        {
            AddReward(1f);
        }
    }
    private void FixedUpdate()
    {
        if (space == true)
        {
            if (targetPos.x > transform.position.x && rb2D.velocity.x < 20)
            {
                rb2D.velocity = new Vector2(rb2D.velocity.x + (0.05f * (20 - rb2D.velocity.x)), rb2D.velocity.y);
            }
            else if (rb2D.velocity.x < -20)
            {
                rb2D.velocity = new Vector2(rb2D.velocity.x - (0.015f * (20 - rb2D.velocity.x)), rb2D.velocity.y);
            }
            if (targetPos.y > transform.position.y && rb2D.velocity.y < 20)
            {
                rb2D.velocity = new Vector2(rb2D.velocity.x, rb2D.velocity.y + (0.025f * (20 - rb2D.velocity.y)));
            }
            else if (rb2D.velocity.y < -20)
            {
                rb2D.velocity = new Vector2(rb2D.velocity.x, rb2D.velocity.y - (0.025f * (20 - rb2D.velocity.y)));
            }
        }
    }
}

And here is the error I'm getting:

ArgumentException: GetComponent requires that the requested component 'CameraSensor' derives from MonoBehaviour or Component or is an interface

Fewer observations (0) made than vector observation size (1). The observations will be padded. UnityEngine.Debug:LogWarningFormat (string,object[]) Unity.MLAgents.Sensors.VectorSensor:Write (Unity.MLAgents.Sensors.ObservationWriter) (at Library/PackageCache/com.unity.ml-agents@2.0.1/Runtime/Sensors/VectorSensor.cs:56)

I'm not sure if this is caused by something I'm missing in my code or if it's a component I'm missing or something like that.

avariant
  • 2,234
  • 5
  • 25
  • 33
Ian Weed
  • 64
  • 8
  • See last answer at following posting : https://stackoverflow.com/questions/68013135/how-to-getcomponent-from-other-gameobject – jdweng Jun 30 '22 at 23:51
  • There was another error I was getting and I added it to the original post. Thank you for responding. – Ian Weed Jun 30 '22 at 23:59
  • Need line where error is occurring and exact error message. – jdweng Jul 01 '22 at 00:03
  • the error is: Fewer observations (0) made than vector observation size (1). The observations will be padded. UnityEngine.Debug:LogWarningFormat (string,object[]) Unity.MLAgents.Sensors.VectorSensor:Write (Unity.MLAgents.Sensors.ObservationWriter) (at Library/PackageCache/com.unity.ml-agents@2.0.1/Runtime/Sensors/VectorSensor.cs:56) – Ian Weed Jul 01 '22 at 00:08
  • You are not getting any data back in response or the response is in a different format than what code needs. At top of my link it says "This question already has been answered". The is a link to another posting which has tons of debug information. – jdweng Jul 01 '22 at 00:29

0 Answers0