Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Need help on detecting when objects above another object are gone

Discussion in 'Scripting' started by RayDawg, Jul 28, 2014.

  1. RayDawg

    RayDawg

    Joined:
    Mar 19, 2013
    Posts:
    108
    I made a post a few weeks ago and was told to look up voxel methods. Needless to say, it produced nothing for me and only led to confusion.

    I have a flat field of 1x1 blocks, and below that, I plan on randomly generating several different size and shape blocks (3x1s, 2x2s, etc).

    I want to be able to destroy the different shaped blocks only after the 1x1 blocks directly above it are gone, but I don't know how to approach it.

    I tried OnCollisionEnter, OnCollisionExit, and some voxel tutorials that led me to something completely different.

    Parenting the 1x1 blocks to the different shaped blocks don't seem to be a good idea since they are being randomly placed underneath the 1x1 block field and I have no idea what to do, could someone help?
     
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    To me, I would think you tap, or mouse click the positon of the box 1x1. This acts as a trigger to instruct any box, directly below it, to destroy itself.. Yes?


    If so, you need to use ray casts hits, to dectect when your input has hit a 1x1 box. Then, I would fire another ray, down, from the position of the box you just tapped, to see if any boxes exist below it. Again, a RaycastHit is you answer.
     
  3. RayDawg

    RayDawg

    Joined:
    Mar 19, 2013
    Posts:
    108

    No, the boxes directly below the 1x1's don't destroy themselves, they have to be clicked on *after* the 1x1's directly above it are destroyed. Then the shapes (the 2x2 and other shapes) are destroyed. Only problem is I don't know how to proceed since I'm going to be randomly spawning them behind the 1x1 field.
     
  4. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Hm.
    Can you provide an image.
    It would help me to help you.
     
  5. RayDawg

    RayDawg

    Joined:
    Mar 19, 2013
    Posts:
    108
    I really don't know any other way to describe it so here you go. As I said, there's a 2x2 block behind four 1x1 blocks. To destroy the 2x2 block, all the 1x1 blocks above it needs to be destroyed first. What I have so far, though, is the 2x2 block destroying itself on click, but I don't know how to have the 2x2 or other uniquely shaped blocks check to see if everything above it is clear.
     

    Attached Files:

  6. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    I would go with a manager type system.

    Iets say your game screen is 10x10,
    Then you can fit 5x5, 2x2 blocks. Correct?

    Now, further, I would make it so that these blocks stay static, in that they are not destroyed and spawned or even moved. If one is destroyed, just turn off its colliders and renderers. This can be said of any block.

    Now, each block should have a block script. In block script, it should have a variable as an array, childrenBlocks. Fill this variable[], with the smaller blocks directly above it. Now, each block has a reference to the smaller blocks associated with it.

    Now, on input, if tap, fire a ray, if ray hits a block, tell the input manager, to contact this block by using SendMessage(). The function it will trigger, in the block script is, thisBlockTapped(). This function will run a check, to see if all the smaller blocks above it have been destroyed. If yes, destroy. If not, do nothing.

    Now, if you can destroy this block, get this block to tell the parentBlock below it, that it has been destroyed. The parent member will have to recieve this message 4 times to know that itself, can be destroyed.

    Get it?
     
    Last edited: Jul 30, 2014