- Home /
Match 3 objects and destroy!?!?!
I cannot get my head around this one. I simply want to match 3 objects of the same type and destroy them all once there are 3 connected. I have tried parenting and destroying when childCount == 3. I have tried fixedJoint.connectedBody. I have tried OnCollisionEnter, if tags are equal, count+=1, if(count>=3), etc.... It works on some connections, but not most. Here is the code I have so far, this one works the best out of what I have tried. Next I will try Raycast, but I want to find if anyone can help w/ this. I have 3 cubes just for testing. They are 1 Red, 1 Green, 1 Blue cube, tagged R, G, and B. They are all non-kinematic rigidbodies, as well as box colliders.
var count : int = 1;
function OnCollisionStay (collision : Collision)
{
if(collision.gameObject.tag == gameObject.tag)
{
count+=1;
collision.transform.parent = transform;
}
if(transform.childCount >= 2 && count>= 3)
{
Destroy(gameObject);
count = 1;
}
}
What you want to do sounds very reasonable, but I just need a bit more information before I can help you. The question I have is: what are the conditions under which you want to destroy the three objects? And related: how do you know which 3 objects to destroy?
For instance, 'I want to destroy 3 cubes when they collide with the player. The 3 cubes are all children of the same object.' Or: 'I want to destroy the closest 3 cubes to the player every 10 seconds.'
It is a tetris style Drop, I made it where a random cube is picked to drop at a random position offset. When 3 cubes of a kind are of the same color, I want all 3 to be destroyed. I have everything working as far as how to detect what cube is what color. The blue cubes only destroy if they touch 2 other blue cubes. Same w/ red and green cubes. It's just that sometimes it works, but it doesn't work if they match vertically or horizontally. It only works if they are in an L shape, like one cube in the center, and a cube on the top or bottom, and another on the right or left. This is because the corners of all 3 cubes are touching, or colliding. In other words, if there are 3 in a row veritcally or horizontally, it doesn't work, because it only works if they are all 3 colliding w/ each other at once. I hope I explained this well enough to understand. It's like the count+1 variable adds up no matter where collisions occur. Should count be a static variable?
Thanks a lot $$anonymous$$r Clunk. You saved my life in a way. your question is very helpful to me. i could not answer your question though. But, your post solved my similar other problem.
What a bump! Would be interesting to know if you got your head around the problem baris150490.
Your answer
Follow this Question
Related Questions
Columns/Magic Jewelry remake on Unity 0 Answers
Pattern Detection 1 Answer
Child object getting transformed out side the scene on collision? 1 Answer
Pixel perfect Collision? 0 Answers
"Collision" between UI images 1 Answer