我有一个宝石(立方体),有球体作为孩子。我如何改变这些宝石和球体以改变它们的位置?我尝试了一些片段来改变他们的立场,但我收到了错误。我该怎样洗牌这些宝石?请帮我解决一下。
Board.cs
public List<Gem> gems = new List<Gem>();
public int GridWidth;
public int GridHeight;
public GameObject gemprefab;
public Gem gemComponent;
public Vector3[] shuffleArray = new Vector3[gems.Count];
// Use this for initialization
void Start () {
ChangePosition();
}
public void ChangePosition() {
for (int i = 0; i < shuffleArray.Length; i++) {
int temp = gems[i].transform.position;
int randomIndex = Random.Range(i, shuffleArray.Length);
gems[i].transform.position = gems[randomIndex];
gems[randomIndex] = temp;
}
}
答案 0 :(得分:1)
试
for(int i = 0; i < gems.Count; i++) {
Vector3 temp = gems[i].transform.position;
int randomIndex = Random.Range(0, gems.Count);
gems[i].transform.position = gems[randomIndex].transform.position;
gems[randomIndex].transform.position = temp;
}