两个脚本:第一个挂在一个空的Gameobject上,然后第二个不用挂,通过第一个调用。
第一个是控制生成,第二个是碰撞锁轴
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour {
public GameObject[] blocks;
private float high = 60f;
private GameObject Parent;
private GameObject Child1;
private GameObject Child2;
private GameObject Child3;
// Use this for initialization
void Start () {
StartCoroutine(SwtchInit(Random.Range(-40f, 28f)));
}
IEnumerator SwtchInit(float x)
{
int numForSwitch;
while(true)
{
x = Random.Range(-40f, 28f);
numForSwitch = Random.Range(1, 18);
yield return new WaitForSeconds(2);
switch(numForSwitch)
{
case 1:
InitBlock1(x);
break;
case 2:
InitBlock2(x);
break;
case 3:
InitBlock3(x);
break;
case 4:
InitBlock4(x);
break;
case 5:
InitBlock5(x);
break;
case 6:
InitBlock6(x);
break;
case 7:
InitBlock7(x);
break;
case 8:
InitBlock8(x);
break;
case 9:
InitBlock9(x);
break;
case 11:
InitBlock11(x);
break;
case 12:
InitBlock12(x);
break;
case 13:
InitBlock13(x);
break;
case 14:
InitBlock14(x);
break;
case 15:
InitBlock15(x);
break;
case 16:
InitBlock16(x);
break;
case 17:
InitBlock17(x);
break;
}
}
}
void InitBlock1(float x)//长条1
{
Parent = Instantiate(blocks[Random.Range(0, 4)],new Vector3(x,high,0),Quaternion.identity);
Parent.AddComponent<Rigidbody2D>();
Parent.AddComponent<DetectionCollison>();
Instantiate(blocks[Random.Range(0, 4)], new Vector3(x+3.45f, high, 0), Quaternion.identity,Parent.transform);
Instantiate(blocks[Ra