产生不同类型的对象

时间:2016-06-27 20:16:54

标签: c# unity3d

我正在尝试创建一个可以生成不同类型对象的脚本

3 个答案:

答案 0 :(得分:1)

从您的描述和评论中听到的是,您希望保证在speiclaCratePercentageMinspeiclaCratePercentageMax百分比的箱子之间是特殊创作,但其余的只能是正常创作。如果是这样的话,你需要做的就是弄清楚百分比将占总数的百分比,首先产生许多,然后用正常的板条箱填充其余部分。

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

public class spawnmanager : MonoBehaviour {

public int noOfobjects = 6;

public Transform[] spawnPoints;  

public GameObject normalCrate;
public GameObject specialCrate;

public float speiclaCratePercentageMin;
public float speiclaCratePercentageMax;

void Awake()
{
}

// Use this for initialization
void Start () 
{
    spawner();
}

void spawner ()
{
    List<Transform> availablePoints = new List<Transform>(spawnPoints);

    //Figures out how many special creates we need.
    int numberOfSpecialCrates = noOfobjects * Random.Range(this.speiclaCratePercentageMin, this.speiclaCratePercentageMax);

    //Added i<spawnPoints.Length check to prevent errors when noOfobjects is bigger than the number of available spawn points.
    for (int i = 0; i<noOfobjects && i<spawnPoints.Length;i++)
    {    
        int spawnPointIndex = Random.Range (0, availablePoints.Count);

        //As long as i is lower than numberOfSpecialCrates we spawn a special crate.
        if(i < numberOfSpecialCrates)
        {
            Debug.Log("dd");
            Instantiate(specialCrate, availablePoints[spawnPointIndex].position, Quaternion.identity);
        } 
        else
        {
            Instantiate(normalCrate, availablePoints[spawnPointIndex].position, Quaternion.identity) ;
        }     

        availablePoints.RemoveAt(spawnPointIndex);
    }
}
}

答案 1 :(得分:0)

我认为问题出在这里 if(randomFloat&gt; = 1 || randomFloat&lt; = 0.9f&amp;&amp; randomFloat&gt; = 0.7f)

更好地使用更多括号

if(randomFloat&gt; = 1 ||(randomFloat&lt; = 0.9f&amp;&amp; randomFloat&gt; = 0.7f))

答案 2 :(得分:0)

查看this SO问题以获取相关提示。这似乎是你在寻找的。