繁体   English   中英

如何控制数组中的所有游戏对象?

[英]How to control all GameObjects in array?

我有一个生成器,每次按 (1) 时都会生成一个球,每个球都将存储在名为“ targetBall ”的游戏对象数组中,我有一个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);
                }
            }
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM