使用Unity随机时间生成物体
要想随机时间生成物体,可以设置一个时间的counter,每次随机赋予一个时间,然后在counter数完时做生成。示例代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UIElements;
public class SpawnManagerX : MonoBehaviour { private float spawnInterval;
void Start() { spawnInterval = Random.Range(minTime, maxTime); }
public float minTime = 1; public float maxTime = 5; private float counter = 0; void Update() { counter += Time.deltaTime; if (counter > spawnInterval) { SpawnRandom(); counter = 0; spawnInterval = Random.Range(minTime, maxTime); }
}
void SpawnRandom () {
}
}
|