public void Explode(int damage) { Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, explosionRadius); StartCoroutine(Boom()); PlayerStats activePlayer = GameManager.activePlayers[whoThrew].GetComponent <PlayerStats>(); foreach (Collider2D nearbyObject in colliders) { Rigidbody2D rb = nearbyObject.GetComponent <Rigidbody2D>(); DestructableObject destr = nearbyObject.GetComponent <DestructableObject>(); if (rb != null && rb.tag == "Player") { rb.gravityScale = 1; rb.AddForce((rb.transform.position - transform.position).normalized * ((1 / (rb.transform.position - transform.position).magnitude) * explosionStrength), ForceMode2D.Impulse); rb.GetComponent <PlayerStats>().TakeDamage(damage); activePlayer.damageDealt += damage; if (rb.GetComponent <PlayerStats>().health <= 0) { activePlayer.damageDealt += rb.GetComponent <PlayerStats>().health; activePlayer.kills++; } } if (destr != null) { destr.Damage(terrainDamage); } } Destroy(gameObject); }
protected virtual void HandleCollision(Collision collision) { if (collision.gameObject.CompareTag("Player")) { if (OnHitObject != null) { OnHitObject(this); } } // Grab the rigidBody of the collision Rigidbody collisionRigidbody = collision.gameObject.GetComponent <Rigidbody>(); // Grab the Health of the collision //Health collisionHealth = collision.gameObject.GetComponent<Health>(); DestructableObject collisionDestructable = collision.gameObject.GetComponent <DestructableObject>(); // If the collision has a rigidBody if (collisionRigidbody != null) { // if this object has health if (health != null) { // Damage this object based of the magnitude of the relative velocity Damage(Mathf.RoundToInt(collision.relativeVelocity.magnitude)); } // if collision object has health //if (collisionHealth!=null) // // Damage collision object based of the magnitude of the relative velocity // collisionHealth.Damage(Mathf.RoundToInt(collision.relativeVelocity.magnitude)); if (collisionDestructable != null) { // Damage collision object based of the magnitude of the relative velocity collisionDestructable.Damage(Mathf.RoundToInt(collision.relativeVelocity.magnitude)); } // If the collision has run out of health //if (collisionHealth!=null && collisionHealth.currentHealth <= 0) // // Break that object // collision.gameObject.GetComponent<DestructableObject>().BreakObject(); // If this object has run out of health //if (health!= null && health.currentHealth <= 0) // // Break this object // BreakObject(); } }
protected override void LeftClick() { anim.Play("Bat Swing"); //base.LeftClick(); Ray ray; RaycastHit hit; ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit, 4)) { DestructableObject destructableObject = hit.collider.gameObject.GetComponent <DestructableObject>(); if (destructableObject != null) { destructableObject.Damage(100); } } }