private void Explode() { Collider[] colliders = Physics.OverlapSphere(transform.position, explosionRadius); foreach (Collider collider in colliders) { HealthData healthData = collider.gameObject.GetComponent <HealthData>(); if (healthData == null && collider.gameObject.transform.parent != null) { healthData = collider.gameObject.transform.parent.GetComponent <HealthData>(); } if (healthData != null) { // target hit healthData.Damage(damageAmount); // create VFX GameObject explosion = Instantiate(explosionVFX, transform.position, Quaternion.identity); explosion.transform.localScale *= explosionScale * collider.gameObject.transform.localScale.x; // SFX audioSource.Stop(); audioSource.clip = null; audioSource.minDistance = explosionSFXMinDistance; audioSource.maxDistance = 750.0f; audioSource.PlayOneShot(audioOnExplosion); Destroy(explosion, 2f); if (rigidbody != null) { Destroy(rigidbody); // stopping movement } gameObject.transform.localScale = new Vector3(0, 0, 0); // hiding object, so SFX plays well Destroy(gameObject, 5f); } } }
public void Setup(Vector3 shootDir, Vector3 initialVelocity, int maxRange, int damageAmount) { this.shootDir = shootDir; rigidbody.velocity = initialVelocity; Destroy(gameObject, 5f); RaycastHit hit; if (Physics.Raycast(transform.position, shootDir, out hit, maxRange)) { if (hit.transform.GetComponent <HealthData>() != null) { // TODO: Wait as much so that the damage is done when the bullet is there at the ship. // Wait a little bit so that the route of the bullet looks realistic WaitSeconds(0.5f); HealthData healthData = hit.transform.GetComponent <HealthData>(); healthData.Damage(damageAmount); GameObject explosion = Instantiate(explosionVFX, hit.point, Quaternion.identity); explosion.transform.localScale *= explosionScale * hit.transform.localScale.x; Destroy(explosion, 2f); } } }