usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;publicclassgrow_b:MonoBehaviour{publicGameObject prefab;List<GameObject> allbody;// Start is called before the first frame updatevoidStart(){
allbody =newList<GameObject>();
allbody.Add(this.gameObject);}// Update is called once per framevoidUpdate(){//GameObject last = Instantiate(prefab, new Vector3(10, 0, 0), Quaternion.identity);Vector3 pos = allbody[allbody.Count -1].transform.position;float gap = allbody[0].transform.position.x +280;bool flag =false;if(gap +1> allbody.Count) flag =true;float dist =(pos.x +280)*(pos.x +280)+(pos.y -60)*(pos.y -60)+(pos.z -118)*(pos.z -118);if(dist >=1&& flag ){GameObject last =Instantiate(prefab,newVector3(pos.x-1, pos.y, pos.z), Quaternion.Euler(0.0f,0.0f,90.0f));
last.transform.parent = allbody[allbody.Count -1].transform;
last.GetComponent<ConfigurableJoint>().connectedBody = allbody[allbody.Count -1].GetComponent<Rigidbody>();
allbody.Add(last);}if(pos.x <-280){GameObject des = allbody[allbody.Count -1];
allbody.RemoveAt(allbody.Count -1);DestroyImmediate(des);}}}