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

2D Environment Destruction and Bombs ?

Discussion in '2D' started by CelticKnight, Nov 10, 2018.

  1. CelticKnight

    CelticKnight

    Joined:
    Jan 12, 2015
    Posts:
    378
    Hello All,

    Having a little more trouble with my current project, this time it's trying the get the correct bomb to explode after throwing it. Yes, you read that correctly :p.

    I was experimenting with my project at getting a bomb to explode after throwing it and doing some damage to the environment, it was working well for one object: pick it up, throw it, have damage happen to the environment. All good, but, there was something gnawing in the back of the brain saying "you've c*cked it up somehow", and sure enough, as soon as I added another another prefab of the bomb to the game, the wrong bomb was blowing up, if I picked up and threw the right bomb the left bomb would blow and vice-versa. I sort of understand why it might be happening, but, I can't understand how to rectify it.

    The script below is getting information that the bomb is being thrown (grabberscript), but, not exactly which of the bombs it refers to (I think) and therefore doesn't know which one should be exploded. I thought that because this script is on the object that it would explode this particular object!?!

    To anyone who can help here is a big thankyou in advance.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class bombXplode : MonoBehaviour {
    6.  
    7.     //grabberScript stuff
    8.     //the script that bold whether the object have been thrown
    9.     private grabberScript gs;
    10.  
    11.     public GameObject xPlosion;
    12.  
    13.     public float blastRadius = 5f;
    14.  
    15.     //get the correct laymask
    16.     //so only destructible element can get blasted to S***house
    17.     public LayerMask destruct;
    18.  
    19.     //get player health
    20.     public Health playerHealth;
    21.     public float timerDelay = 2f;
    22.  
    23.     //access to PlayFX in GameManager
    24.     private GameObject soundLink;
    25.  
    26.     // Use this for initialization
    27.     void Start () {
    28.         print("in bombXplode Script");
    29.         playerHealth = GetComponent<Health>();
    30.  
    31.         gs = FindObjectOfType<grabberScript>();
    32.  
    33.         soundLink = GameObject.Find("GameManager");
    34.     }
    35.    
    36.  
    37.  
    38.     // Update is called once per frame
    39.     void Update ()
    40.     {
    41.         //check if obj has been thrown
    42.         if (gs.isThrown == true )
    43.         {
    44.             ///print("isThown: " + gs.isThrown);
    45.             gs.isThrown = false;
    46.             StartCoroutine(Xplode());
    47.         }
    48.     }
    49.  
    50.  
    51.     void OnTriggerEnter2D(Collider2D collision)
    52.     {
    53.         //print("AniBomb activated");
    54.         //StartCoroutine(Xplode());
    55.     }
    56.  
    57.  
    58.     public IEnumerator Xplode()
    59.     {
    60.        GameObject clone;
    61.         yield return new WaitForSeconds(timerDelay);
    62.  
    63.         //wait 2 seconds
    64.         print("bomb will go off");
    65.         //instantiate prefab - particle FX
    66.         clone = Instantiate(xPlosion, this.transform.position, Quaternion.identity);
    67.  
    68.         Collider2D[] objsDestroy = Physics2D.OverlapCircleAll(transform.position, blastRadius);
    69.         //Collider2D[] objsDestroy = Physics2D.OverlapCircleAll(transform.position, blastRadius, destruct);
    70.  
    71.         //play sound
    72.         soundLink.GetComponent<PlayFX>().playFX(0);
    73.  
    74.  
    75.         foreach (Collider2D blah in objsDestroy)
    76.         {
    77.             if (blah.name == "GroundCheck")
    78.             {
    79.                 print("Do nothing");
    80.             }
    81.             else if (blah.tag == "Player")
    82.             {
    83.                 blah.GetComponent<Health>().TakeHealth(10);
    84.                 //Destroy(blah.gameObject);
    85.             }
    86.             else
    87.             {
    88.                 Destroy(blah.gameObject);
    89.             }
    90.         }
    91.  
    92.         Destroy(clone.gameObject, .5f);
    93.         Destroy(this.gameObject);
    94.  
    95.         yield return 0;
    96.     }
    97.  
    98.  
    99.  
    100.     private void OnDrawGizmosSelected()
    101.     {
    102.         Gizmos.color = Color.red;
    103.         Gizmos.DrawWireSphere(this.transform.position, blastRadius);
    104.     }
    105.  
    106.  
    107. }//end bombXplode
    108.  
     
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    all bomb can check this bool ? if (gs.isThrown == true )
     
  3. CelticKnight

    CelticKnight

    Joined:
    Jan 12, 2015
    Posts:
    378
    yeah they can:oops:. I'm pretty much self taught "hobbyist" programmer, so, I come up with some pretty out there stuff. That's the reason for all these questions. With all my other projects I've eventually ran into the wall and had to stop but I'm pushing on with this one - I've had the restart this project 3 times because for some reason the project died and stopped working.

    I'm trying to find a way that I can check which bomb has been picked up, so the proper one can be destroyed!
     
  4. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    try to do something from the thrower/grabber script like: neededBomb.bombXplode.canExplode = true; (or with public function: neededBomb.bombXplode.StartExplode ();
     
    Last edited: Nov 11, 2018
  5. CelticKnight

    CelticKnight

    Joined:
    Jan 12, 2015
    Posts:
    378
    I see where you were coming from with that idea and I sort of tried going down that road but it still didn't take into account that there was more than one prefab onscreen and which was being used. I tried many different little things to try and find which prefab was actually being thrown. I finally found an approach that did work and it needed all the code on the bomb being bundled into the grabberScript. I didn't want to do that, I really wanted to keep those objects separate but at least it works and can go around throwing all the bombs I want :p.