using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//
class AutomobilePart
{
public GameObject obj; //
public int id; //ID
public float maxArea; //
public float distance; //
public byte OrientCode;
public Transform. initTransform; //
public Transform. targTransform;
public AutomobilePart(GameObject obj, int id, Transform. initTransform, float maxArea)
{
this.obj = obj;
this.id = id;
this.initTransform. = initTransform;
this.maxArea = maxArea;
this.targTransform. = new GameObject().transform;
}
}
public class DemoV2 : MonoBehaviour
{
public GameObject Sphere;
//
private List automobileParts;
private void Start()
{
initData();
//initBoxCollider();
}
private void Update()
{
//
if (Input.GetKeyUp(KeyCode.A))
{
//StartCoroutine("PlayExplodeAnimation");
//
PlayExplodeAnimationStep();
}
//
}
//
void initData()
{
//
automobileParts = new List();
MeshRenderer[] allChildren = GetComponentsInChildren();
int index = 1; //ID
foreach (MeshRenderer child in allChildren)
{
child.gameObject.AddComponent();
child.gameObject.AddComponent();
child.gameObject.GetComponent().useGravity = false; //
child.gameObject.GetComponent().isKinematic = true; //
//
BoxCollider bc = child.gameObject.GetComponent();
float maxArea = Mathf.Max(bc.bounds.size.x * bc.bounds.size.y, bc.bounds.size.z * bc.bounds.size.y, bc.bounds.size.x * bc.bounds.size.z);
automobileParts.Add(new AutomobilePart(child.gameObject, index++, child.transform, maxArea));
//Debug.Log(child.name + " maxArea=" + maxArea);
}
//
automobileParts.Sort(delegate(AutomobilePart x, AutomobilePart y)
{
return y.maxArea.CompareTo(x.maxArea);
});
Debug.Log("after area sorting...");
foreach (AutomobilePart child in automobileParts)
{
Debug.Log(child.obj.name + " maxArea(after)=" + child.maxArea);
}
Debug.Log("The static object is " + automobileParts[0].obj.name);
changeMaterial(automobileParts[0].obj);
//
for (int i = 1; i ();
meshrender.material.color = new Color(1, 0, 0, 1.0f);
}
float calcDistance(AutomobilePart based, AutomobilePart attached)
{
BoxCollider bc1 = based.obj.GetComponent();
Vector3 baseCenter = bc1.bounds.center;
BoxCollider bc2 = attached.obj.GetComponent();
Vector3 attaCenter = bc2.bounds.center;
Vector3 diff = attaCenter - baseCenter;
return Mathf.Abs(diff.magnitude);
}
void initOrientation(AutomobilePart based, AutomobilePart attached)
{
BoxCollider bc1 = based.obj.GetComponent();
BoxCollider bc2 = attached.obj.GetComponent();
Vector3 baseCenter = bc1.bounds.center;
Vector3 attaCenter = bc2.bounds.center;
Vector3 diff = attaCenter - baseCenter;
attached.OrientCode = 0;
if (diff.x > 0)
attached.OrientCode |= 0x001;
else if(diff.x 0)
attached.OrientCode |= 0x004;
else if (diff.y 0)
attached.OrientCode |= 0x010;
else if (diff.z ();
attached.targTransform.position = attached.initTransform.position;
Vector3 ffset = new Vector3(diff.x * 5.0f, diff.y * 5.0f, diff.z * 5.0f);
attached.targTransform.position += offset;
}
//
void initBoxCollider()
{
//
BoxCollider bc = gameObject.AddComponent();
BoxCollider bc_origin = bc;
bc.center = new Vector3(bc_origin.center.x, bc_origin.center.y + bc_origin.size.y * 10.0f, bc_origin.center.z);
bc.size = new Vector3(bc_origin.size.x, 0, bc_origin.size.z);
//
bc = gameObject.AddComponent();
bc.center = new Vector3(bc_origin.center.x, bc_origin.center.y - bc_origin.size.y * 10.0f, bc_origin.center.z);
bc.size = new Vector3(bc_origin.size.x, 0, bc_origin.size.z);
//
bc = gameObject.AddComponent();
bc.center = new Vector3(bc_origin.center.x - bc_origin.size.x * 10.0f, bc_origin.center.y, bc_origin.center.z);
bc.size = new Vector3(0, bc_origin.size.y, bc_origin.size.z);
//
bc = gameObject.AddComponent();
bc.center = new Vector3(bc_origin.center.x + bc_origin.size.x * 10.0f, bc_origin.center.y, bc_origin.center.z);
bc.size = new Vector3(0, bc_origin.size.y, bc_origin.size.z);
//
bc = gameObject.AddComponent();
bc.center = new Vector3(bc_origin.center.x, bc_origin.center.y, bc_origin.center.z - bc_origin.size.z * 10.0f);
bc.size = new Vector3(bc_origin.size.x, bc_origin.size.y, 0);
//
bc = gameObject.AddComponent();
bc.center = new Vector3(bc_origin.center.x, bc_origin.center.y, bc_origin.center.z + bc_origin.size.z * 10.0f);
bc.size = new Vector3(bc_origin.size.x, bc_origin.size.y, 0);
}
private IEnumerator PlayExplodeAnimation()
{
Debug.Log("step...............");
int i = automobileParts.Count - 1;
while (i > 0)
{
Rigidbody rb = automobileParts[i].obj.GetComponent();
rb.isKinematic = false;
/////
float dis = calcDistance(automobileParts[0], automobileParts[i]);
Debug.Log(automobileParts[i].obj.name + " to base dis=..............."+ dis);
if (dis >= 5.0f * automobileParts[i].distance)
{
Debug.Log(automobileParts[i].obj.name + " removed rigidbody.");
Destroy(rb);
}
i--;
yield return new WaitForSeconds(0.0005f);
}
}
private void PlayExplodeAnimationStep()
{
float speed = 4f;
Debug.Log("step...............");
for(int i=1; i();
if (rb)
{
float dis = calcDistance(automobileParts[0], automobileParts[i]);
Debug.Log(automobileParts[i].obj.name + " to base dis=..............." + dis);
automobileParts[i].obj.transform.position = Vector3.MoveTowards(automobileParts[i].obj.transform.position, automobileParts[i].targTransform.position, speed * Time.deltaTime);
if (dis >= 1.5f * automobileParts[i].distance)
{
Debug.Log(automobileParts[i].obj.name + " removed rigidbody.");
Destroy(rb);
}
}
}
}
}