- Home /
destroy all same color combination of bricks
Hi Everyone
I am making brick puzzle game.i am facing a problem please help me
you can see what type of project I am doing.Here is my question is whenever my ball collide or hit any red colored brick all the red colored brick which collide each other will destroy.i can't find any solution to do that.how i can destroy all the red colored brick which collide each other.Please suggest me the right ans. how i can destroy same colored combination of bricks.
and sorry for my English
thanks in advance
I think that you could use raycast to all neighbours, and then check the render.material.color $$anonymous$$aybe this could works. ;)
What is your code like? What you should be able to do is use a boolean as well, so it looks at whether or not the red ones are touching, and destroys them if they are, but I can't be too useful without your code.
If each brick is its own object it might be easier to assign a Tag and compare the Tag against collision. For example, give the red bricks a Tag of "red" so when you're checking for collision write an if statement to check if the collision object has a tag of "red", if so, destroy the game object.
@AtomicPlatypus, If you do that, you would destroy every red object on the level at once, rather than just those that are touching. I think the OP just wants to destroy those that are touching.
@Bloodyem $$anonymous$$y intention was not to destroy by Tag but simply use the Tag to assess if a red brick was in the collision. You could then use what the collider is hitting to destroy that specific game object, not all red Tagged objects. Perhaps I wasn't as clear as I could have been. ;)
Answer by robertbu · May 31, 2013 at 04:22 PM
You could put something like this on all your boxes. Note it assumes that the boxes are tagged "Red" and "Green" for red and green boxes:
#pragma strict
function OnCollisionEnter(col : Collision) {
if (col.collider.name != "Ball")
return;
var tag = gameObject.tag;
var hit : RaycastHit;
// Check the boxes above
while(Physics.Raycast(transform.position, Vector3.up, hit)) {
if (hit.collider.tag != tag)
break;
hit.collider.enabled = false;
Destroy(hit.collider.gameObject);
}
// Check the boxes below
while(Physics.Raycast(transform.position, Vector3.down, hit)) {
if (hit.collider.tag != tag)
break;
hit.collider.enabled = false;
Destroy(hit.collider.gameObject);
}
Destroy(gameObject);
}
Answer by temptest123 · May 31, 2013 at 04:34 PM
doing anything with raycast here is overkill, this should be handled in the datamodel. Store references to your boxes in a 2D array for example (as you have rows of boxes). Or create a linked list implementation in which each box has a reference to the top and bottom box.
Depends on the nature of the game and how the boxes are stacked. There won't be a performance issue with using raycasting for any reasonable number of boxes. And neither solution (raycasting or data model) would work well for some stacking scenarios.
Your answer
Follow this Question
Related Questions
Internal collisions 1 Answer
2D box colliders gap 0 Answers
Multiple Colliders Trigger problem 2 Answers
Collider going through walls with box collider.. 1 Answer
Weird physic effect if object collides in collider intersection 0 Answers