C# 尝试在半随机位置反复生成预制件

C# 尝试在半随机位置反复生成预制件,c#,unity3d,C#,Unity3d,因此,我编写这段代码是为了开始编写一个脚本,在我的场景中随机生成灌木丛对象,但是在运行时,它只生成第一个灌木丛。代码如下: using System.Collections; using System.Collections.Generic; using UnityEngine; public class BushSpawner : MonoBehaviour { public GameObject bush; private float x = 0f; privat

因此,我编写这段代码是为了开始编写一个脚本,在我的场景中随机生成灌木丛对象,但是在运行时,它只生成第一个灌木丛。代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BushSpawner : MonoBehaviour
{

    public GameObject bush;
    private float x = 0f;
    private float y = -.47f;
    private float z = 0f;
    private int bushCount = 0;
    private Vector3 origPos;
    private bool xPlus = false;
    private bool xMinus = false;
    private bool zPlus = false;
    private bool zMinus = false;
    // Use this for initialization
    void Start()
    {
        SpawnBushes();
    }

    // Update is called once per frame
    void Update()
    {

    }

    void SpawnBushes()
    {
        Vector3 startPos = new Vector3(x, y, z);
        Instantiate(bush, startPos, Quaternion.identity);
        bushCount += 1;
        while (bushCount < 100)
        {
            Vector3 checkPos = new Vector3(x, y, z);
            Collider[] intersecting = Physics.OverlapSphere(checkPos, 1f);
            if (intersecting.Length == 0)
            {
                //code to run if nothing is intersecting as the length is 0
                Instantiate(bush, checkPos, Quaternion.identity);
                bushCount += 1;
            }
            else
            {
                //code to run if something is intersecting it
                RollPos();
            }
        }
    }
    void RollPos()
    {
        if (xPlus == true
            && xMinus == true
            && zPlus == true
            && zMinus == true)
        {
            int newRoll = Random.Range(1, 4);
            if (newRoll == 1)
            {
                x += 10f;
            }
            else if (newRoll == 2)
            {
                x -= 10f;
            }
            else if (newRoll == 3)
            {
                z += 10f;
            }
            else if (newRoll == 4)
            {
                z -= 10f;
            }
            xPlus = false;
            xMinus = false;
            zPlus = false;
            zMinus = false;
        }
        else
        {
            int roll = Random.Range(1, 4);
            if (roll == 1)
            {
                if (xPlus == false)
                {
                    x += 2f;
                    xPlus = true;
                }
                else
                {
                    RollPos();
                }
            }
            if (roll == 2)
            {
                if (xMinus == false)
                {
                    x -= 2f;
                    xMinus = true;
                }
                else
                {
                    RollPos();
                }
            }
            if (roll == 3)
            {
                if (zPlus == false)
                {
                    z += 2f;
                    zPlus = true;
                }
                else
                {
                    RollPos();
                }
            }
            if (roll == 4)
            {
                if (zMinus == false)
                {
                    z -= 2f;
                    zMinus = true;
                }
                else
                {
                    RollPos();
                }
            }
        }
    }
}
我尝试在bool为true时将spawnbush置于更新中运行,然后在spawnbush完成时将其设为false,但这会创建第一个bush,然后在它旁边的一个随机位置创建99个其他bush


如果有人能给我指出正确的方向或告诉我我完全偏离了方向,我将不胜感激

Ron Beyer指出我在RollPos中的Random.range中没有足够大的范围。再次感谢你,罗恩

然后在它旁边的一个随机位置上放置99个其他灌木。也就是说99个灌木丛在同一位置?是的,在同一位置。每个灌木丛都有碰撞器,但没有刚体灌木丛计数曾经达到100还是卡在循环中?灌木丛计数确实达到100,并在更新时生成100个灌木丛,但灌木丛的位置在第一次位置更改后停止更改RollPos应该做什么?为预制件选择一个随机位置?应该注意的是,Random.Range1,4永远不会返回4,因此您只选择1-3。那么zMinus将永远不会为true,并且RollPos中if语句的第一部分将永远不会运行。