ML代理中相机传感器的问题

发布于 2025-02-12 05:31:36 字数 2723 浏览 3 评论 0 原文

我正在尝试使用ML代理进行AI,但我似乎无法让相机传感器实际提供输入。我已经遵循了几个教程,但没有一个人足够深入,如果他们做的话,它们已经过时了。这是我的代码:

    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)));
            }
        }
    }
}

这是我遇到的错误:

参数exception:getComponent需要请求的组件 “摄影师”源自Monobehaviour或组件,或者是 接口

比矢量观测大小(1)进行的观察值(0)。这 观察将被填充。 UnityEngine.debug:logwarningformat (String,Object [])unity.mlagents.sensors.vectorsensor:写入 (unity.mlagents.sensors.observationwriter)(at 库/packagecache/

我不确定这是由我在代码中缺少的东西还是我缺少的组件或类似的内容引起的。

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/[email protected]/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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文