Unity我的实例化算法有什么问题?

时间:2017-10-07 09:13:12

标签: c# unity3d

我不知道我是否可以调用此算法。但是我正在制作一个玩家将在圆形路径中移动的游戏。 my project image

正如你在图片中看到的那样,玩家可以绕圈运行。并且障碍物应该在圆圈中实例化。我试图在上半部分(左边是长立方体)然后在下半部分创建障碍物。但是当代码不应该这样做时,事情也会在下半年创建。此外,它显示参数异常错误。请查看我的代码并告诉我我的方法是错误的还是我的公式是错误的还是其他任何内容。

   public class ObjectInstantiater : MonoBehaviour {
    DataHolder dataholder;
    GameObject Obstacle;
    LevelData leveldata;

    private int currentlevel=0; // default level starts from 0
    private List<GameObject> Inactivegameobject = new List<GameObject>(); // this object can be used
    private List<GameObject> Activegameobject = new List<GameObject>();

    private int totalgameobjects;
    private int firsthalfgameobjects, secondhalfgameobjects;
    public float outerradius;
    public float innerradius;

    private bool shallspawnouterradiues = true;
    // Use this for initialization
    void Awake () {
        dataholder = (Object)GameObject.FindObjectOfType<DataHolder>() as DataHolder;
        Obstacle = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
        leveldata = dataholder.Leveldata[0];
    }

    void Start()
    {
        Updateleveldata();
        FirstHalf();
    }
    public int Currentlevel
    {
        get { return currentlevel; }
        set { currentlevel = value;
            leveldata = dataholder.Leveldata[currentlevel];//sets the level data
        }
    }

    private void Updateleveldata() // this function gets called after a round
    {
        totalgameobjects = Random.Range(leveldata.MinimumObstacle, leveldata.MaximumObstacle);
        firsthalfgameobjects = Mathf.RoundToInt(totalgameobjects / 2);
        secondhalfgameobjects = totalgameobjects - firsthalfgameobjects;

    }
    private void FirstHalf()
    {
        Debug.Log(firsthalfgameobjects);
        Vector3 pos;
        if (Inactivegameobject.Count < firsthalfgameobjects)
        {
            for (int x = 0; x <= (firsthalfgameobjects - Inactivegameobject.Count); x++)
            {
                GameObject obs = Instantiate(Obstacle) as GameObject;
                obs.SetActive(false);
                Inactivegameobject.Add(obs);
            }
        }
        float spawnangledivision = 180 / firsthalfgameobjects;
        float spawnangle = 180f;
        for(int x = 0; x < firsthalfgameobjects; x++)
        {
            float proceduralRandomangle = spawnangle;
             proceduralRandomangle = Random.Range(proceduralRandomangle , proceduralRandomangle + 2f);
            if (shallspawnouterradiues)
            {
                 pos = new Vector3(outerradius * Mathf.Cos(spawnangle), outerradius * Mathf.Sin(spawnangle), 0f);
                shallspawnouterradiues = false;
            }else
            {
                 pos = new Vector3(innerradius * Mathf.Cos(spawnangle), innerradius * Mathf.Sin(spawnangle), 0f);
                shallspawnouterradiues = true;
            }
            spawnangle += spawnangledivision;
            Inactivegameobject[0].SetActive(true); // set it to 0 
            Inactivegameobject[0].transform.position = pos;
            Activegameobject.Add(Inactivegameobject[0]);
            Inactivegameobject.RemoveAt(0);
        }

    }

    private void SecondHalf()// No need to check this
    {
        if (Inactivegameobject.Count < firsthalfgameobjects)
        {
            GameObject obs = Instantiate(Obstacle) as GameObject;
            obs.SetActive(false);
            Inactivegameobject.Add(obs);
        }
    }



}

0 个答案:

没有答案