I want to make an exploding block but don't know how [SOLVED]

Okay, my goal is to make an exploding block that damages all nearby blocks in a certain radius. Here is what I thought I would do:

-When the block is destroyed, call a method called something like “explosion” that would:
-Instantiate a circle collider
-check for collisions in collider
-damage all breakable blocks in collider once

I have searched and searched and searched for tutorials on how to do these things, but have come up emptyhanded for the most part. Can anyone suggest ways to do this?

I think you are on to it. If you instantiate a collider and the do OverlapColliderYou can loop Colliders and destroy/damage them

Okay, here is what I have so far; having a tough time recognizing a collision between the explosion and neighboring blocks. If anyone has any suggestions I am all ears. It doesn’t throw any errors, but doesn’t work either:

private void Explosion()
{
    {
        gameObject.AddComponent<CircleCollider2D>();
        myExplosion = GetComponent<CircleCollider2D>();
        myExplosion.radius = explosionRadius;
        Debug.Log("Explosion Successful!");
        
        void OnCollisionEnter(Collision collision)
        {
            if (collision.gameObject.tag == "Breakable")
            {
                Debug.Log("Explosion Successful");
                HandleHit();
            }
        }

If you are using 2D physics you need to make sure its called OnCollisionEnter2D. Maybe that is the issue.

I tried that after you posted, but it didn’t work. I think I have tried just about everything at this point, collision logic may come natural to some, but definitely not to me.

Does the first debug post? Try putting a Debug.Log inside the Collision but outside of the if statement to see if the issue is with the collision itself or the if statement.

Yeah the first debug log outputs just fine. It’s the collision part that isn’t working. I can instantiate the circle collider just fine, and I have even paused the game and SEEN the circle collider, so I know it is working. I just can’t get other blocks to recognize that they colliding with the circle collider and that they need to be blown up! Here is what I am trying now. One method to instantiate the explosion circle collider. This part works fine:

private void Explosion()
{
    {
        gameObject.AddComponent<CircleCollider2D>();
        myExplosion = GetComponent<CircleCollider2D>();
        myExplosion.isTrigger = true;
        myExplosion.radius = explosionRadius;
        Debug.Log("Explosion Successful!");

    }

}

And another method to check for a collision with a circle collider:

private void OnTriggerEnter2D(Collider2D collider2D)
{
    Debug.Log("PLZ WORK");
}

I have been trying to distinguish this collision detection from the collision detection between the ball and the block by making the explosion collider a “trigger”, but that hasn’t worked so well yet. What I have noticed is that if I turn off the destroy part of the block hit like this:

private void DestroyBlock()
{
    AudioSource.PlayClipAtPoint(blockDestroy, Camera.main.transform.position);
    TriggerSparklesVFX();
    Explosion();
    FindObjectOfType<GameSession>().AddToScore();
    level.BlockDestroyed();
    //Destroy(gameObject);
}

Then I can actually get the PLZ WORK message to show up. So the block may be getting destroyed too fast before it can actually cause an explosion. I don’t know man. This is driving me nuts. Hope some part of this makes sense.

You can have the explosion block create a new game object when it gets destroyed and have the new game object handle the explosion before it is destroyed.

Yeah i thought of that too, but I can’t quite get the command right. I am trying to use the Object.Instantiate (https://docs.unity3d.com/ScriptReference/Object.Instantiate.html) command, but when I use it my blocks don’t destroy themselves anymore and I don’t know why lol

private void Explosion()
{
    {
        gameObject.AddComponent<CircleCollider2D>();
        myExplosion = GetComponent<CircleCollider2D>();
        myExplosion.isTrigger = true;
        myExplosion.radius = explosionRadius;
        Instantiate(myExplosion, transform.position, transform.rotation);
        Destroy(myExplosion, .5f);
        //Debug.Log("Explosion Successful!");
    }

}

When I use the above code, my Destroy(gameObject); command stops working

private void DestroyBlock()
{
    AudioSource.PlayClipAtPoint(blockDestroy, Camera.main.transform.position);
    TriggerSparklesVFX();
    Explosion();
    FindObjectOfType<GameSession>().AddToScore();
    level.BlockDestroyed();
    Destroy(gameObject);
}

Well, this is where things stand at the moment. I have tried and tried to get this right, but to no avail. If anyone can come along and show me how to do this that would be awesome, but for the moment, I am just going to put this on the shelf and move on to other lessons. I wish I could get this to work, but maybe future lessons will help me understand how to do it. Thanks for the suggestions yall have given thus far.

So i had a slight issue making the explosion just now as well wondering what was wrong, then I just added a Rigibody to the explosion prefab and it worked.

Scripts:

Summary
  public void OnCollisionEnter(Collision collision)
    {
        Block_Life--;

        if (Block_Life <= 0)
        {
            Game_State.AddToPlayerScore(points);
            Explosion();
            Level.BlockDestroyed();

            AudioSource.PlayClipAtPoint(MyAudioClip, Camera.main.transform.position);
            DropItem();
            Destroy(gameObject);

        }

    }

 public void Explosion() // Will cause an explosion when the block is destoryed.
    {
        if (bombBlock == true)
        {
            Instantiate(explosion, this.transform.position, this.transform.rotation);

            Debug.Log("Explosion!!");

        }
    }

Explosion script

float timeLeft = 5f;

 // Update is called once per frame
    void Update()
    {
       
    timeLeft -= Time.deltaTime;

      if (timeLeft < 0)
      {
         Destroy(gameObject); // Will destroy the explosion game objext in 5 seconds.
      }
      
    }

Images:

Summary

Omg it works!!! David, you are the MAN!! Thank you so much! I have been trying to get this working for days! Appreciate the advice man!

:+1:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms