unity > bcca生成コード (失敗編)

http://qiita.com/7of9/items/6391ed9803d82db2f4a0
をベースとして、cluster-cluster aggregate生成コードを作ってみている。

1. SpherePrefab: Scale(0.3, 0.3, 0.3)

```csharp:SphereControl.cs
using UnityEngine;
using System.Collections;

public class SphereControl : MonoBehaviour {
	
	void Update () {
	}

	private const string kFinish = "Finish";

	void OnCollisionEnter(Collision col)
	{
		if(col.transform.tag.Equals(kFinish)) {
			foreach(Transform child in gameObject.transform.parent) {				   
				if (child.transform.tag.Equals(kFinish)) {
					continue;
				}
				print (child.name);
				Rigidbody rb = child.gameObject.GetComponent<Rigidbody> ();
				rb.isKinematic = true;
				child.gameObject.tag = kFinish;
			}
		}
	}
}
```

```csharp:ButtonPushed.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ButtonPushed : MonoBehaviour {
	
	public GameObject prefab;
	private GameObject monomer;
	private Rigidbody rig1;
	private float s_dist = 6.0f; // arbitrary distance

	private GameObject myParent;

	private List<GameObject> listGO;

	void Start () {
		listGO = new List<GameObject> ();

		myParent = new GameObject ();
		myParent.name = "Parent";

		// make sphere positioned at the center
		monomer = Instantiate (prefab, new Vector3 (0, 0, 0), Quaternion.identity) as GameObject;
		monomer.name = "monomer1";
		monomer.transform.parent = myParent.transform;
		SetChildTagFinish (myParent);

		// to be fixed at the position
		Rigidbody rb = monomer.GetComponent<Rigidbody> ();
		rb.isKinematic = true;
	}

	void SetChildTagFinish(GameObject obj1)
	{
		obj1.tag = "Finish";
		foreach(Transform child in obj1.transform) {
			child.gameObject.tag = "Finish";
		}
	}
	
	float MyRandom(float begin, float end)
	{
		// TODO: use other random number generator having good probability (MT?)
		return Random.Range (begin, end);
	}
	
	void AddMonomer() {
		float theta_rad = MyRandom(0.0f, 2.0f * Mathf.PI);
		float phi_rad = MyRandom(0.0f, Mathf.PI);
		float xpos = s_dist * Mathf.Sin(phi_rad) * Mathf.Cos(theta_rad);
		float ypos = s_dist * Mathf.Sin(phi_rad) * Mathf.Sin(theta_rad);
		float zpos = s_dist * Mathf.Cos(phi_rad);

		int count = myParent.transform.childCount;
		for (int idx=0; idx < count; idx++) {
			Vector3 pos1 = new Vector3 (xpos, ypos, zpos) + myParent.transform.GetChild(idx).position;
			monomer = Instantiate (prefab, pos1, Quaternion.identity) as GameObject;
			monomer.name = "monomer1";
			monomer.transform.parent = myParent.transform;
			listGO.Add(monomer);
		}
	}
		
	void OnGUI() {
		if (GUI.Button (new Rect (10, 10, 100, 40), "Generate")) {
			SetChildTagFinish(myParent);
			AddMonomer();

			// AddForce to push the monomer directed to the center sphere
			for(int idx=0; idx < listGO.Count; idx++) {
				GameObject work = listGO[idx];
				rig1 = work.GetComponent<Rigidbody>();
				rig1.mass = 0.3f;
				print (work.transform.position);
				rig1.AddForce(-work.transform.position * 30f);
			}
		}
	}
}
```
![Scene_unity_-_150809_bcca_v0_9_-_PC__Mac___Linux_Standalone__Personal_.jpg](https://qiita-image-store.s3.amazonaws.com/0/32870/86a8a1ab-acc3-6a31-6e7f-69c8b1d6c939.jpeg)

どうも衝突合体した時に多少粒子が移動してしまっているのか、隙間ができている。
改良の余地あり。