如何设置生成的预制件之间的距离?

时间:2019-07-10 08:10:04

标签: c# unity3d

我想生成预制对象,但是现在对象生成在一个位置上繁殖。我在播放器周围设置了4个位置,并使用此代码生成了对象:

public class EnemySpawner : MonoBehaviour {

    public static EnemySpawner instance;

    [SerializeField]
    private int scoreMileStone = 100;                   //this will define number of cars in the scene
    private int milestoneIncreaser = 100;               //this sets new milestone once old is reached

    [SerializeField]
    private Transform[] spawnPos;                       //store all the available spawn position

    [SerializeField]
    private int policeCarRequired;                      //this will tell how much cars are needed in the scene at a time

    private int currentPoliceCar;                       //this variables keep track of total number of cars in the scene
    private GameObject target;                          //store player reference in this variable
    private int lastPosition, r;

    private int lastSpawnPos;

    public int CurrentPoliceCar { get { return currentPoliceCar; } set { currentPoliceCar = value; } }  //getter and setter

    // Use this for initialization
    void Awake ()
    {
        if (instance == null) instance = this;
    }

    // Update is called once per frame
    void Update ()
    {   
        if (GuiManager.instance.GameStarted == false || GuiManager.instance.GameOver == true)
            return;

        if (target == null)                                         //if target is null
        {
            target = GameObject.FindGameObjectWithTag("Player");    //try detecting target again
            return;                                                 //return from the method
        }

        MilestoneIncreaser();                                       //increase milestone

        if (currentPoliceCar < policeCarRequired)                   //if currentPoliceCar is less than policeCarRequired
        {
            SpawnPoliceCar();                                       //spawn police car
        }

    }

    void SpawnPoliceCar()                                           //spawn police car
    {
        GameObject wolf = ObjectPooling.instance.GetPooledObject("Wolf");     //get police car reference from objectpooling
        RandomPos();

        int r = Random.Range (0, spawnPos.Length);
        while(lastSpawnPos == r)
            r = Random.Range (0, spawnPos.Length);

        wolf.transform.position = new Vector3(spawnPos[r].position.x, 0, spawnPos[r].position.z);  //set the transform
        wolf.SetActive(true);                                                      //set it active in scene
        wolf.GetComponent<Damage>().DefaultSetting();                              //call DefaultSettings method
        lastSpawnPos = r;
        currentPoliceCar++;                                                             //increase currentPoliceCar by 1
    }

    void MilestoneIncreaser()                                               //increase the milestone  
    {
        if (GuiManager.instance.Score >= scoreMileStone)                    //if currentScore is greater or equal to scoreMilestone
        {
            scoreMileStone += milestoneIncreaser;                           //increase the milestone 

            if (policeCarRequired < 8)                                      //if max policeCarRequired is less than 8
                policeCarRequired++;                                        //increase policeCarRequired by 1
        }
    }

    void RandomPos()
    {
        int r = Random.Range(0, spawnPos.Length);                                       //get random number between zero and total spawnpos

        while(lastPosition == r)
            r = Random.Range(0, spawnPos.Length);
    }
}

这给了我这些物体,但它们之间没有任何间隔,例如,一个物体在一个位置生成4个,而在另一个位置生成3个,但它们被挤压了。我可以通过某种方式解决此问题吗?

0 个答案:

没有答案