/// <summary> /// Shatters the object into smaller pieces /// Spawn new game object instances and sends them flying away from the force /// </summary> /// <param name="explosionPos">Position of the explosion origin</param> /// <param name="power"> Power of the explosion force</param> /// <param name="upwardsForce">Additive power added upwards during explosion</param> public void Shatter(Vector3 explosionPos, float power, float upwardsForce) { if (shattered || !canShatter) { return; } shattered = true; //Instantiate new objects List <GameObject> newObjects = new List <GameObject>(); for (int e = 0; e < pieceObjects.Length; e++) { var newObject = Instantiate(pieceObjects[e]); //Set the position showed by the gizmos if (pieceSpawnLocations.Length >= e) { Vector3 localPosition = (this.transform.right * pieceSpawnLocations[e].x) + (this.transform.up * pieceSpawnLocations[e].y); newObject.transform.position = this.transform.position + new Vector3( localPosition.x, localPosition.y, 0.0f); //if(newObject.name.ToLower().Contains("wood")) //print(newObject.transform.position + " " + localPosition); //newObject.transform.localRotation = this.transform.localRotation; //this.transform.right } newObjects.Add(newObject); } Destroy(this.gameObject); //Add explosion force to new objects foreach (var newObject in newObjects) { Rigidbody2D rb = newObject.GetComponent <Rigidbody2D>(); if (rb != null) { //print(explosionPos); //if (newObject.name.ToLower().Contains("wood")) // print(explosionPos + " " + newObject.transform.position); Vector2 force = UtilityLibrary.CalculateExplosionForce(explosionPos, newObject.transform.position, power, upwardsForce); rb.AddForce(force, ForceMode2D.Impulse); } } }
private IEnumerator Explode() { yield return(new WaitForSeconds(delay)); Vector3 explosionPos = this.transform.position; //Spawn explosion animation //Probably not the right way to do this if (explosion != null) { Instantiate(explosion, explosionPos, Quaternion.identity); } Collider2D[] destroyColliders = Physics2D.OverlapCircleAll(explosionPos, destroyRadius); foreach (Collider2D hit in destroyColliders) { Rigidbody2D rb = hit.GetComponent <Rigidbody2D>(); if (rb != null) { Destroy(hit.transform.gameObject); } } //Remove hitpoints on these Collider2D[] damageColliders = Physics2D.OverlapCircleAll(explosionPos, damageRadius); foreach (Collider2D hit in damageColliders) { if (hit.gameObject.tag.Contains("ShatteringObject")) { var shatteringObject = hit.gameObject.GetComponent <ShatteringObject>(); if (shatteringObject != null) { //Remove hitpoints shatteringObject.hitpoints--; if (shatteringObject.hitpoints <= 0) { shatteringObject.Shatter(explosionPos, 100, 100); } } } else if (hit.gameObject.tag.Contains("NPCBuilding")) { var npcHouse = hit.gameObject.GetComponent <NPCBuilding>(); if (npcHouse != null) { npcHouse.DamageBuilding(1, false); } } } Collider2D[] colliders = Physics2D.OverlapCircleAll(explosionPos, radius); //Objects inside main explosive radius. Shatter, destroy, add force foreach (Collider2D hit in colliders) { if (hit.gameObject.tag.Contains("ShatteringObject")) { var shatteringObject = hit.gameObject.GetComponent <ShatteringObject>(); if (shatteringObject != null) { shatteringObject.Shatter(explosionPos, power, upwardsForce); } } else if (hit.gameObject.tag.Contains("NPCBuilding")) { var npcHouse = hit.gameObject.GetComponent <NPCBuilding>(); if (npcHouse != null) { //Destroy NPC buildings if they are hit by the blast npcHouse.DamageBuilding(10000, false); } } else { Rigidbody2D rb = hit.GetComponent <Rigidbody2D>(); if (rb != null) { Vector2 force = UtilityLibrary.CalculateExplosionForce(explosionPos, hit.transform.position, power, upwardsForce); rb.AddForce(force, ForceMode2D.Impulse); } } } Destroy(this.gameObject); }