Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Get abstract script from Collider2D for Bomb script

Discussion in '2D' started by MoonJellyGames, Jun 14, 2015.

  1. MoonJellyGames

    MoonJellyGames

    Joined:
    Oct 2, 2014
    Posts:
    324
    Hey all. I'm having trouble getting my bomb script working. The bomb itself is a simple round sprite with a CircleCollider2D and a Rigidbody2D. The stage is made up of a few blocks which, when fully-functional, will be destructible. Currently, there is only one type of destructible block, "dirt", which has three sprites for various stages of damage: untouched, slightly damaged, and heavily damaged. The sprite will change based on the condition (just an integer) on that block and once the condition has reached zero, the block will disappear (and eventually, I'll get a debris emitter to fire off for a better effect).

    I plan on having many objects that will be effected by a bomb's explosion using either the damage (blastPower - distance from explosion), the directional force of the explosion, or both, but of course, the effect will be different depending on the object. When hit with an explosion, the bomb script should call "TakeExplosionDamage(Vector2 forceDirection, int damage)" on the hit object.

    Only objects in a clear direct line from the explosion will take damage. There may be a better way to implement this than what I've done, but here's the pseudo-code description of my current set-up:

    Bomb class

    has a List<Collider2D> hits
    call Explode()

    make kinematic true (so that the bomb doesn't fall through the ground when the next line happens)
    make CircleCollider2D disabled (so that this bomb isn't hit by its own explosion)
    call DetermineHitsInRadius()

    do a Physics2D.OverlapCircleAll with a radius of the desired blast radius and add each collider hit to the hits list
    call DetermineHits()
    for each Collider2D in hits, do a raycast from this bomb to that object. If the raycastHit2D object is different from the collider in hits, remove that collider from hits as it must be hidden behind something.
    call SendExplosionMessages() (this is where the trouble is)
    for each Collider2D in hits, call TakeExplosionDamage

    I have an abstract class AbstractExplosive (which, of course, is derived from MonoBehaviour) from which any explosive object in the game is derived from. This is where the TakeExplosionDamage function comes from. Pretty much every type of terrain block will be derived from AbstractExplosive, as will the Player class, and even the Bomb class (as bombs can be exploded when caught in the explosion of other bombs).

    So the problem is that I don't know how I can call TakeExplosionDamage on each object in the hits list. I've tried a bunch of different things, but I'm at the point where I just need some advice from the pros. What would you suggest?

     
  2. Gardes

    Gardes

    Joined:
    Apr 7, 2015
    Posts:
    46
    It's a bit confusing for me.. Do you want to know how to access TakeExplosionDamage or is the "hits list" you are talking about an array and you need help to do this function at the array list?

    A full code of an object with your try would be nice to understand what you need :)