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. Dismiss Notice

"Explosion", Its Effect on Other Game Objects, And the Communication Between Them (Questions)

Discussion in 'Scripting' started by MrPriest, Sep 15, 2014.

  1. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    Hey.
    I've got a few questions.
    The general story is that, there are game objects with rigidbody, and an exploding game object.

    "Explosion" has a radius of damage, and a greater radius of shockwave.
    The shockwave force diminishes the farther the impacted game object is from the core of the explosion, and it is the same with the damage.
    The game objects have their FSM, which include "get hurt" and "get shockwave" (Yeah... I need better names, never mind that for now).

    First Question;
    I want to calculate the effect of the blast on a game object, according to it's distance from the core of the blast.
    I have thought of two major ways.
    One - Create a sphere object that grows in scale, having a scale for the damaging radius, as well as a scale for the shockwave radius. The growth is determined with parameters, basically making sure that the farther the object is from the blast, he will get affected later. On trigger enter the spheres affect the colliding objects that have a rigidbody.
    Two - Overlap spheres. The issue here is that they do not grow with the blast growth. It will not matter with small blasts (most of them!) but what if I wanted a bomb that grows slow, or fast? Also I can't really use the overlap spheres as the explosion like I can with the spheres.

    Which is more efficient? Which is recommended? Do you have any other, better alternatives?

    Second Question;
    How should I get the affected game objects to receive the damage/shockwave?
    Here, I also have two methods.
    One - Send message. It's viable, I guess. It's slow, but it should be okay.
    But... I can't send many parameters (unless I put them in an object), and again, they are slow. I am afraid that overusing it will cause the game to slow down.
    Two - Interfaces. You check to see if the rigidbody has an interface that allows it to get shockwaved or damaged, and then send the commands.
    But... After looking around I've only found a way to do it with "GetComponent" which is bad.
    It works great, but again, the way it could affect the game negatively bothers me.

    I don't have the code on me at the moment, but if you require it, I'll post it here in a few hours.
    Basically, how do you make your objects interact?
    I need to present my game in a week, and basically, I'm too much of a code perfectionist and got stuck on the code. I've also learned many new things during my work, and now I am working with FSMs, which makes me work even slower! But I just can't think of using anything else right now. I bite more than I can chew, I am aware of it.

    Thanks in advance!
     
    HosseinArabbeigi likes this.
  2. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    Seriously? I see people answering repeated questions, even silly questions that can be solved by reading a definition of a command, but this one has nothing?
    I would really appreciate the help you know...
    I know no one is obliged to answer me, but I can't give up so easily, you know?

    Also, I've thought of another method.
    On start of each object I list them inside a static list, so I can have interfaced objects already managed, and no need to call "getComponent" every time, but just check if the collider's parent is inside the list that contains the "damageable" or "pushable" or whatever interface.
     
  3. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,935
    I dont think there is 1 correct answer, all of those would work,
    if one method has problems or is too slow in your specific scene & platform, then have to try the other one..

    There was also some ray(cast) based explosion system in the forum/asset store while ago,
    it checks for objects between explosion, so that force doesnt affect through walls for example..
     
  4. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    I have based the explosion on a trigger collider type in gameobject blast.

    All objects must have the Rigidbody component to work, including the gameobject blast with particle system.

    The gameobject blast has activated all constraints in Rigidbody component.

    Gameobject blast script: I modify scale transform to change range explosion:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(SphereCollider))]
    5. [RequireComponent(typeof(Rigidbody))]
    6. [RequireComponent(typeof(ParticleSystem))]
    7. public class ExplosionDamage : MonoBehaviour {
    8.  
    9.     public float maxScale = 8f; //Maximum explosion range
    10.     public float velocity = 2f; //Explosion velocity
    11.  
    12.     float scale = 1f;
    13.  
    14.     void Start(){
    15.  
    16.         //Collider as trigger
    17.         this.collider.isTrigger = true;
    18.  
    19.     }
    20.  
    21.     void Update () {
    22.  
    23.         //Modify Transform Scale, to change explosion range.
    24.         scale += velocity*Time.deltaTime;
    25.  
    26.         if(this.scale>this.maxScale) Destroy(this.gameObject);
    27.         else this.transform.localScale = Vector3.one * this.scale;
    28.    
    29.     }
    30.  
    31. }
    Actors script: detect trigger collision:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. [RequireComponent(typeof(Rigidbody))]
    6. public class Actors : MonoBehaviour {
    7.  
    8.     void OnTriggerEnter(Collider col){
    9.  
    10.         //Detect collision with particle trigger
    11.         if(col.name.Equals("Explosion")){
    12.  
    13.             Debug.Log(this.gameObject.name+" - Time: "+Time.time);
    14.             //You can get position particle, to calculate distance.
    15.             float distance = Vector3.Distance(col.transform.position,transform.position);
    16.  
    17.         }
    18.  
    19.     }
    20.  
    21. }

    Download link.
     
  5. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    That's interesting, having the collision detection on the actor itself... But in that case, the code will be multiplied by how many types of actors I have. Is that okay to do so?
    I am sure that in a small project that would be fine though.
    I'll check out that possibility as well, thanks.
     
  6. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    Also used for large projects.

    Just add the script to GameObjects o Actors need.

    And you can also assign a specific layer for the explosion, and configure the physic matrix to detect collisions with other layers.
     
  7. TobiasjBurford

    TobiasjBurford

    Joined:
    Feb 17, 2022
    Posts:
    11
    There is now a method for this if anyone is still looking for an easy answer for this follow this tutorial: