如何控制数组中的所有GameObject?

我有一个生成器,每次我按下(1)键时就会生成一个球,每个球都会被存储在一个名为”targetBall“的Gameobjects数组中。我有一个AI玩家,它使用Move()方法来排斥球,但问题是AI玩家只能看到数组中的第一个球,即代码中显示的球[0],我该如何让AI玩家看到所有生成的球(无限个球)?我尝试使用for循环但没有成功(注意:其他一切都运行得很完美)

void Move()    {        targetBall = GameObject.FindGameObjectsWithTag("HokeyBall");            if (targetBall[0].GetComponent<HokyBall>().ballDirection == Vector3.right)            {                ballPos = targetBall[0].transform.localPosition;                if (transform.localPosition.x < Lboundry && ballPos.x > transform.localPosition.x)                {                    transform.localPosition += new Vector3(speed * Time.deltaTime, 0, 0);                }                if (transform.localPosition.x < Lboundry && ballPos.x < transform.localPosition.x)                {                    transform.localPosition += new Vector3(-speed * Time.deltaTime, 0, 0);                }            }}

这是我尝试使用for循环查找每个生成的球的方法,但出现了错误(无法将“int”转换为“GameObject”)

void Move()        {            targetBall = GameObject.FindGameObjectsWithTag("HokeyBall");foreach (GameObject i in targetball){                    if (targetBall[i].GetComponent<HokyBall>().ballDirection == Vector3.right)                {                    ballPos = targetBall[i].transform.localPosition;                        if (transform.localPosition.x < Lboundry && ballPos.x > transform.localPosition.x)                    {                        transform.localPosition += new Vector3(speed * Time.deltaTime, 0, 0);                    }                    if (transform.localPosition.x < Lboundry && ballPos.x < transform.localPosition.x)                    {                        transform.localPosition += new Vector3(-speed * Time.deltaTime, 0, 0);}                    }                }    }

回答:

foreach返回集合中的对象,因此你无法访问该集合。

使用foreach的方法 –

    void Move()    {        targetBall = GameObject.FindGameObjectsWithTag("HokeyBall");        foreach (GameObject go in targetball)        {            if (go.GetComponent<HokyBall>().ballDirection == Vector3.right)            {                ballPos = go.transform.localPosition;                if (transform.localPosition.x < Lboundry && ballPos.x > transform.localPosition.x)                {                    transform.localPosition += new Vector3(speed * Time.deltaTime, 0, 0);                }                if (transform.localPosition.x < Lboundry && ballPos.x < transform.localPosition.x)                {                    transform.localPosition += new Vector3(-speed * Time.deltaTime, 0, 0);                }            }        }    }

使用for循环的方法 –

    void Move()    {        targetBall = GameObject.FindGameObjectsWithTag("HokeyBall");        for (int i = 0; i < targetBall.Length; i++)        {            if (targetBall[i].GetComponent<HokyBall>().ballDirection == Vector3.right)            {                ballPos = targetBall[i].transform.localPosition;                if (transform.localPosition.x < Lboundry && ballPos.x > transform.localPosition.x)                {                    transform.localPosition += new Vector3(speed * Time.deltaTime, 0, 0);                }                if (transform.localPosition.x < Lboundry && ballPos.x < transform.localPosition.x)                {                    transform.localPosition += new Vector3(-speed * Time.deltaTime, 0, 0);                }            }        }    }

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注