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.