- Home /
How do I check that no collisions have a tag?
I'm trying to make a match3 game, and based on how i've implement it, every time a tile changes position, it'll try to group up with any similar colored tiles it's now adjacent to. However, this also means I need to make it ungroup from them. Is there any way to check all collisions simultaneously to determine that none have the same tag? I'd want to do something like
private void OnCollisionExit(Collider2D[4] other)
{
if(other[0].gameObject.tag != this.gameObject.tag && other[1].gameObject.tag != this.gameObject.tag && other[2].gameObject.tag != this.gameObject.tag && other[3].gameObject.tag != this.gameObject.tag)
{
this.transform.parent = null;
}
}
But I'm not sure if that would work, as the standard collision functions only check one collision at a time, and I can't think of a good way to keep track that none of them have been the same type/tag as the current tile.
Answer by Buckslice · Jan 13, 2018 at 01:06 AM
Does each tile have a box collider + rigidbody and you are just letting them fall into a grid pattern and then try to find out which ones are near each other? Seems unreliable... I wouldn't even use the physics engine at all. Just store the tiles in a 2D array and each time a tile is moved do a search from each tile to see if it is apart of a match. I feel like thats how most of these kind of games would handle this.
Wouldn't that slow down the game due to the time required to search? If a match only happens when a tile moves, then i should be able to get away with seeing if the moved tile is part of a group of 3+ (i.e. if its new parent has 2 or more descendants). Seems much more efficient than running a linear search through the whole grid
Ya only search from the tiles that moved, good idea. Still I wouldn't use the physics engine for this. Not sure what you are talking about with parent and descendants though... there's no real hierarchical structure here.
$$anonymous$$y basic idea was that I'd set the moving tile as a child of any of its kin that it lies next to. So that all adjacent tiles of the same color are in the same family and can be easily destroyed.
Your answer
Follow this Question
Related Questions
How to set collision for an object with specific collider size? 3 Answers
2D colliders not work 1 Answer
Tile collision by sprite is not accurate with Tilemap Collider 0 Answers
OnCollisionExit2D workaround? 0 Answers
What does it mean to be "grounded"? 0 Answers