protected void CreateNewGameObjects(IList <IHull> newHulls, out GameObject[] newGameObjects) { // Get new meshes Mesh[] newMeshes = new Mesh[newHulls.Count]; float[] newVolumes = new float[newHulls.Count]; float totalVolume = 0.0f; for (int i = 0; i < newHulls.Count; i++) { Mesh mesh = newHulls[i].GetMesh(); Vector3 size = mesh.bounds.size; float volume = size.x * size.y * size.z; newMeshes[i] = mesh; newVolumes[i] = volume; totalVolume += volume; } MeshFilter meshFilter = GetComponent <MeshFilter>(); MeshCollider meshCollider = GetComponent <MeshCollider>(); Rigidbody rigidbody = GetComponent <Rigidbody>(); // Remove mesh references to speed up instantiation //meshFilter.sharedMesh = null; //if (meshCollider != null) //{ // meshCollider.sharedMesh = null; //} // Create new game objects newGameObjects = new GameObject[newHulls.Count]; for (int i = 0; i < newHulls.Count; i++) { IHull newHull = newHulls[i]; Mesh newMesh = newMeshes[i]; float volume = newVolumes[i]; GameObject newGameObject = (GameObject)Instantiate(gameObject); newGameObject.tag = "cubeClone"; /* Set position */ newGameObject.transform.position = gameObject.transform.position; // Set shatter tool ShatterTool newShatterTool = newGameObject.GetComponent <ShatterTool>(); if (newShatterTool != null) { newShatterTool.hull = newHull; } // Set mesh filter MeshFilter newMeshFilter = newGameObject.GetComponent <MeshFilter>(); if (newMeshFilter != null) { newMeshFilter.sharedMesh = newMesh; } // Set mesh collider MeshCollider newMeshCollider = newGameObject.GetComponent <MeshCollider>(); if (newMeshCollider != null) { newMeshCollider.sharedMesh = newMesh; } // isTrigger //newMeshCollider.isTrigger = true; // Set rigidbody Rigidbody newRigidbody = newGameObject.GetComponent <Rigidbody>(); if (rigidbody != null && newRigidbody != null) { newRigidbody.mass = .1f; newRigidbody.constraints = RigidbodyConstraints.None; //newRigidbody.constraints = RigidbodyConstraints.FreezePositionY; if (!gameObject.GetComponent <Bullet>()) { float randomX = Random.Range(-10, 10f); newRigidbody.AddForce(new Vector3(randomX, 20f, 0f)); } else { } //if (!newRigidbody.isKinematic) //{ // newRigidbody.velocity = rigidbody.GetPointVelocity(newRigidbody.worldCenterOfMass); // newRigidbody.angularVelocity = rigidbody.angularVelocity; //} } //newGameObject.tag = "DestroyedSpike"; // Update properties newShatterTool.CalculateCenter(); newGameObjects[i] = newGameObject; } }
protected void CreateNewGameObjects(IList <IHull> newHulls, out GameObject[] newGameObjects) { // Get new meshes Mesh[] newMeshes = new Mesh[newHulls.Count]; float[] newVolumes = new float[newHulls.Count]; float totalVolume = 0.0f; for (int i = 0; i < newHulls.Count; i++) { Mesh mesh = newHulls[i].GetMesh(); Vector3 size = mesh.bounds.size; float volume = size.x * size.y * size.z; newMeshes[i] = mesh; newVolumes[i] = volume; totalVolume += volume; } MeshFilter meshFilter = GetComponent <MeshFilter>(); MeshCollider meshCollider = GetComponent <MeshCollider>(); Rigidbody rigidbody = GetComponent <Rigidbody>(); // Remove mesh references to speed up instantiation meshFilter.sharedMesh = null; if (meshCollider != null) { meshCollider.sharedMesh = null; } // Create new game objects newGameObjects = new GameObject[newHulls.Count]; for (int i = 0; i < newHulls.Count; i++) { IHull newHull = newHulls[i]; Mesh newMesh = newMeshes[i]; float volume = newVolumes[i]; GameObject newGameObject = (GameObject)Instantiate(gameObject); // Set shatter tool ShatterTool newShatterTool = newGameObject.GetComponent <ShatterTool>(); if (newShatterTool != null) { newShatterTool.hull = newHull; } // Set mesh filter MeshFilter newMeshFilter = newGameObject.GetComponent <MeshFilter>(); if (newMeshFilter != null) { newMeshFilter.sharedMesh = newMesh; } // Set mesh collider MeshCollider newMeshCollider = newGameObject.GetComponent <MeshCollider>(); if (newMeshCollider != null) { newMeshCollider.sharedMesh = newMesh; } // Set rigidbody Rigidbody newRigidbody = newGameObject.GetComponent <Rigidbody>(); if (rigidbody != null && newRigidbody != null) { newRigidbody.mass = rigidbody.mass * (volume / totalVolume); if (!newRigidbody.isKinematic) { newRigidbody.velocity = rigidbody.GetPointVelocity(newRigidbody.worldCenterOfMass); newRigidbody.angularVelocity = rigidbody.angularVelocity; } } // Update properties newShatterTool.CalculateCenter(); newGameObjects[i] = newGameObject; } }