using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spin : MonoBehaviour
{
public GameObject prefabToRotate;
[Range(1, 100)]
public int numberOfObjects = 5;
[Range(1, 500)]
public float[] speeds;
public bool randomNumbersOfObjects = false;
public bool randomSpeed = false;
private List<GameObject> instantiatedObjects = new List<GameObject>();
// Start is called before the first frame update
void Start()
{
speeds = new float[numberOfObjects];
if(randomNumbersOfObjects == true)
{
numberOfObjects = Random.Range(1, 100);
}
if(randomSpeed == true)
{
for(int i = 0; i < speeds.Length; i++)
{
speeds[i] = Random.Range(1, 500);
}
}
for(int i = 0; i < numberOfObjects; i++)
{
GameObject go = Instantiate(prefabToRotate);
instantiatedObjects.Add(go);
}
}
// Update is called once per frame
void Update()
{
for (int i = 0; i < numberOfObjects; i++)
{
instantiatedObjects[i].transform.Rotate(Vector3.down, speeds[i] * Time.deltaTime);
}
}
}
如何从“范围”滑块获取随机数和随机速度? 1,100和1,500?我还希望能够在“更新”中更改滑块的此值,并且它将在运行游戏时实时更新对象数量和随机速度。
答案 0 :(得分:1)
将“速度”数组的长度设置为“ numberOfObjects”,然后更改“ numberOfObjects”的值,但“速度”数组仍等于“ numberOfObjects”的旧值。尝试在为'numberOfObjects'分配一个随机值之后,在 中设置'speeds'的长度,
if (randomNumbersOfObjects == true)
{
numberOfObjects = Random.Range(1, 100);
}
speeds = new float[numberOfObjects];