生成不同类型的对象
本文关键字:对象 同类型 | 更新日期: 2023-09-27 17:51:20
我想创建一个脚本,在那里我可以产生不同类型的对象
从你的描述和评论听起来像是你想要保证在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);
}
}
}
我认为问题在这里if (randomFloat>=1 || randomFloat <=0.9f &&randomFloat>= 0.7f)
最好使用更多括号
if (randomFloat>=1 || (randomFloat <=0.9f &&
看看这个问题。