See if an object is in contact with any number of a certain type of object

Hey guys, I am making a ‘Jenga’ game, and i am trying to detect when the player has completely pulled a piece out of the tower. I need to check whether there are any other blocks touching the current block. There can be blocks above, bellow, left and right of the current block.

I wanted to use something like a Box Cast in these directions, and check if I hit any blocks. But unity doesnt have a box cast! I also tried to put 4 box collider/triggers on all sides, but I cant just query a trigger whether or not its in contact with something at a given time. I have to use OnTriggerX Functions and this makes it impossible to track whether or not a block is hitting another, since a block may hit more than 1 block.

As @Julien.Lynge syasm OnCollisionStay(), Enter and Exit could work for you.

I suggest that you:

  • Have a hash within the script for each block
  • For each OnCollisionEnter() you can add the collider to the hash
  • Remove colliders in OnCollisionExit()
  • When you remove a collider you can check to see if the size of the hash is 0

You could maybe do a similar thing with OnCollisionStay() building a hash each frame and in LateUpdate() checking to see the size of the hash.

Hope that helps