我有15个立方体和1个空游戏对象,我想要将它们改组,以便在游戏开始时改变它们的位置。实际上我正在尝试制作15个益智游戏。但是当多维数据集切换它们的位置时会遇到问题,它们会相互重叠。请帮助我解决这个问题。
movement.js
function Start ()
{
ChangePosition();
AssignPositions();
}
function ChangePosition()
{
cube1.transform.position = new Vector3(Random.Range (1,4),Random.Range (1,4),10);
cube2.transform.position = new Vector3(Random.Range (1,4),Random.Range (1,4),10);
cube3.transform.position = new Vector3(Random.Range (1,4),Random.Range (1,4),10);
cube4.transform.position = new Vector3(Random.Range (1,4),Random.Range (1,4),10);
cube5.transform.position = new Vector3(Random.Range (1,4),Random.Range (1,4),10);
cube6.transform.position = new Vector3(Random.Range (1,4),Random.Range (1,4),10);
cube7.transform.position = new Vector3(Random.Range (1,4),Random.Range (1,4),10);
cube8.transform.position = new Vector3(Random.Range (1,4),Random.Range (1,4),10);
cube9.transform.position = new Vector3(Random.Range (1,4),Random.Range (1,4),10);
cube10.transform.position = new Vector3(Random.Range (1,4),Random.Range (1,4),10);
cube11.transform.position = new Vector3(Random.Range (1,4),Random.Range (1,4),10);
cube12.transform.position = new Vector3(Random.Range (1,4),Random.Range (1,4),10);
cube13.transform.position = new Vector3(Random.Range (1,4),Random.Range (1,4),10);
cube14.transform.position = new Vector3(Random.Range (1,4),Random.Range (1,4),10);
cube15.transform.position = new Vector3(Random.Range (1,4),Random.Range (1,4),10);
}
function AssignPositions()
{
for (var i = 0; i < cubesPosition.length; ++i)
cubeGameObjects[i].transform.position = cubesPosition[i];
}
答案 0 :(得分:-1)
您的代码会混洗您的cube数组,该数组是Vector3类型的元素数组。这只是解决问题所需的一半操作。在您对该数组(您将其视为多维数据集的位置数组)进行混洗后,您需要将这些位置实际分配给您的多维数据集(游戏对象)。要做到这一点,您需要有一个立方体的游戏对象列表。像这样:
var cubes: Vector3[] = blablabla;
var cubeGameObjects: GameObject[] = blablabla; //it's up to you how to fill this array
function ChangePosition() {
//your code goes here
}
function AssignPositions() {
for (var i = 0; i < cubes.length; ++i)
cubeGameObjects[i].transform.position = cubes[i];
}
function Start ()
{
ChangePosition();
AssignPositions();
}
我建议您将cubes
的名称更改为cubesPositions
。