Beispiel #1
0
    public void Explode()
    {
        // Make the pickup spawner start to deliver a new pickup.
        //pickupSpawner.StartCoroutine(pickupSpawner.DeliverPickup());

        // Find all the colliders on the Enemies layer within the bombRadius.
        Collider2D[] enemies = Physics2D.OverlapCircleAll(transform.position, bombRadius, 1 << LayerMask.NameToLayer("Enemies"));

        // For each collider...
        foreach (Collider2D en in enemies)
        {
            // Check if it has a rigidbody (since there is only one per enemy, on the parent).
            Rigidbody2D rb = en.GetComponent <Rigidbody2D>();
            if (rb != null && rb.tag == "Enemy")
            {
                // Find the Enemy script and set the enemy's health.
                rb.gameObject.GetComponent <Enemy>().HP -= bombDamage;


                if (rb.gameObject.GetComponent <Enemy>().HP <= 0)
                {
                    PowerUpManager.BombKill();
                }

                // Find a vector from the bomb to the enemy.
                Vector3 deltaPos = rb.transform.position - transform.position;

                // Apply a force in this direction with a magnitude of bombForce.
                Vector3 force = deltaPos.normalized * bombForce;
                rb.AddForce(force);
            }
        }

        // Set the explosion effect's position to the bomb's position and play the particle system.
        explosionFX.transform.position = transform.position;
        explosionFX.Play();

        // Instantiate the explosion prefab.
        Instantiate(explosion, transform.position, Quaternion.identity);

        // Play the explosion sound effect.
        AudioSource.PlayClipAtPoint(boom, transform.position);

        // Destroy the bomb.
        Destroy(gameObject);
    }