我正在尝试创建一个可以生成不同类型对象的脚本
答案 0 :(得分:1)
从您的描述和评论中听到的是,您希望保证在speiclaCratePercentageMin
和speiclaCratePercentageMax
百分比的箱子之间是特殊创作,但其余的只能是正常创作。如果是这样的话,你需要做的就是弄清楚百分比将占总数的百分比,首先产生许多,然后用正常的板条箱填充其余部分。
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问题以获取相关提示。这似乎是你在寻找的。 p>