C# 物体在彼此之间出现

C# 物体在彼此之间出现,c#,unity3d,C#,Unity3d,我为玩家设置了一个衍生障碍物,当游戏开始时,障碍物彼此出现。我试图通过while和physics 2d循环解决这个问题,但当我开始游戏时,Unity崩溃了。 我还尝试通过Raycast2D检查光束何时与某种对撞机碰撞,并改变其位置。但是雷阿斯莫尔并不总是管用 第一条路 while (!Physics.CheckBox(pos, BlotPref[1].transform.localScale)) { pos = new Vector2(Random.Range(-1f

我为玩家设置了一个衍生障碍物,当游戏开始时,障碍物彼此出现。我试图通过while和physics 2d循环解决这个问题,但当我开始游戏时,Unity崩溃了。 我还尝试通过Raycast2D检查光束何时与某种对撞机碰撞,并改变其位置。但是雷阿斯莫尔并不总是管用

第一条路

 while (!Physics.CheckBox(pos, BlotPref[1].transform.localScale))
    {
         pos = new Vector2(Random.Range(-1f, 1f), YPosSetter());
    }
第二条路

 while (hit.collider == null)
    {
        transform.position = new Vector2(Random.Range(-1f, 1f), YPosSetter());
    }
产卵器

private void SpawnerPaper()
{

    for (int i = 0; i < PlaningSpawn; i++)
    {
        if (rndType <= _dbPaperSpawn)
        {
            var blot = Instantiate(BlotPref[Random.Range(0, BlotPref.Length)], new Vector2(Random.Range(-1f, 1f), Random.Range(30f, 70f)), Quaternion.identity);
            blot.transform.SetParent(paper.transform);
        }
    }
}
private void SpawnerPaper()
{
for(int i=0;i如果(rndType一种解决方案是测试碰撞器是否在随机位置附近,使用并使用:

当然障碍物有对撞机

private void SpwanerPaper()
{
    for (int i = 0; i < PlaningSpawn; i++)
    {
        if (rndType <= _dbPaperSpawn)
        {
            while (true)
            {
                var position = new Vector2(Random.Range(-1f, 1f), Random.Range(30f, 70f));
                 // i have choosen radius 1f, you could increase it if needed
                Collider2D[] colliders = Physics2D.OverlapCircleAll(position, 1f, obstacleLayer);

                if (colliders.Length == 0)
                {
                    //so no obstacles present, spawn here
                    var blot = Instantiate(BlotPref[Random.Range(0, BlotPref.Length)],
                        position, Quaternion.identity);
                    blot.transform.SetParent(paper.transform);
                    break;// get out while loop
                }

                // so  obstacles within 1m of the player loop again and give new random position

            }
        }

    }
}
private void SpwanerPaper()
{
for(int i=0;i如果(rndType一种解决方案是测试碰撞器是否在随机位置附近,使用并使用:

当然障碍物有对撞机

private void SpwanerPaper()
{
    for (int i = 0; i < PlaningSpawn; i++)
    {
        if (rndType <= _dbPaperSpawn)
        {
            while (true)
            {
                var position = new Vector2(Random.Range(-1f, 1f), Random.Range(30f, 70f));
                 // i have choosen radius 1f, you could increase it if needed
                Collider2D[] colliders = Physics2D.OverlapCircleAll(position, 1f, obstacleLayer);

                if (colliders.Length == 0)
                {
                    //so no obstacles present, spawn here
                    var blot = Instantiate(BlotPref[Random.Range(0, BlotPref.Length)],
                        position, Quaternion.identity);
                    blot.transform.SetParent(paper.transform);
                    break;// get out while loop
                }

                // so  obstacles within 1m of the player loop again and give new random position

            }
        }

    }
}
private void SpwanerPaper()
{
for(int i=0;i如果(RND如果我理解你随机产生障碍?有时你会重叠?是的,对不起我的英语不好!你能显示你的繁殖代码吗?好的,当然一分钟!如果我理解你随机产生障碍?有时你会重叠?是的,对不起我的英语不好!你能显示你的繁殖代码吗?好的,当然一分钟!Unity被粉碎:DUnity is:D