我的随机化器脚本出现 Unity 2D 问题
Unity 2D trouble with my randomiser script
我创建了这个脚本来随机生成我的按钮,但不知道如何更改它们单独生成的时间,因为它们都是同时生成的。我看不到它们,也没有生成,但是在层次结构中正在创建它们。
Public class RandomSpawn: MonoBehaviour {
public GameObject prefab1, prefab2, prefab3, prefab4, prefab5;
public float spawnRate = 2f;
float nextSpawn = 0f;
int whatToSpawn;
// Update is called once per frame
void Update() {
if (Time.time > nextSpawn)
{
whatToSpawn = Random.Range(1, 6);
switch (whatToSpawn)
{
case 1:
Instantiate(prefab1, transform.position, Quaternion.identity);
break;
case 2:
Instantiate(prefab2, transform.position, Quaternion.identity);
break;
case 3:
Instantiate(prefab3, transform.position, Quaternion.identity);
break;
case 4:
Instantiate(prefab4, transform.position, Quaternion.identity);
break;
case 5:
Instantiate(prefab5, transform.position, Quaternion.identity);
break;
}
nextSpawn = Time.time + spawnRate;
}
我错过了什么?
使用Time.deltaTime
值,
https://docs.unity3d.com/ScriptReference/Time-deltaTime.html
private float _t = 0;
private SpawnRate = 2;
void Update() {
_t += Time.deltaTime;
if (_t > SpawnRate)
{
whatToSpawn = Random.Range(1, 6);
switch (whatToSpawn)
{
case 1:
Instantiate(prefab1, transform.position, Quaternion.identity);
break;
case 2:
Instantiate(prefab2, transform.position, Quaternion.identity);
break;
case 3:
Instantiate(prefab3, transform.position, Quaternion.identity);
break;
case 4:
Instantiate(prefab4, transform.position, Quaternion.identity);
break;
case 5:
Instantiate(prefab5, transform.position, Quaternion.identity);
break;
}
_t = 0;
}
我创建了这个脚本来随机生成我的按钮,但不知道如何更改它们单独生成的时间,因为它们都是同时生成的。我看不到它们,也没有生成,但是在层次结构中正在创建它们。
Public class RandomSpawn: MonoBehaviour {
public GameObject prefab1, prefab2, prefab3, prefab4, prefab5;
public float spawnRate = 2f;
float nextSpawn = 0f;
int whatToSpawn;
// Update is called once per frame
void Update() {
if (Time.time > nextSpawn)
{
whatToSpawn = Random.Range(1, 6);
switch (whatToSpawn)
{
case 1:
Instantiate(prefab1, transform.position, Quaternion.identity);
break;
case 2:
Instantiate(prefab2, transform.position, Quaternion.identity);
break;
case 3:
Instantiate(prefab3, transform.position, Quaternion.identity);
break;
case 4:
Instantiate(prefab4, transform.position, Quaternion.identity);
break;
case 5:
Instantiate(prefab5, transform.position, Quaternion.identity);
break;
}
nextSpawn = Time.time + spawnRate;
}
我错过了什么?
使用Time.deltaTime
值,
https://docs.unity3d.com/ScriptReference/Time-deltaTime.html
private float _t = 0;
private SpawnRate = 2;
void Update() {
_t += Time.deltaTime;
if (_t > SpawnRate)
{
whatToSpawn = Random.Range(1, 6);
switch (whatToSpawn)
{
case 1:
Instantiate(prefab1, transform.position, Quaternion.identity);
break;
case 2:
Instantiate(prefab2, transform.position, Quaternion.identity);
break;
case 3:
Instantiate(prefab3, transform.position, Quaternion.identity);
break;
case 4:
Instantiate(prefab4, transform.position, Quaternion.identity);
break;
case 5:
Instantiate(prefab5, transform.position, Quaternion.identity);
break;
}
_t = 0;
}