I am creating a game where I need collision between blocks of the same colour to be acted upon. The blocks are children of gameobjects which act to create clusters of blocks - kind of like Tetris shapes. The rigidbody component thus belongs to the parent gameobject and OnCollisionEnter is called only when two parents collide. I have no problem identifying when matching blocks collide when OnCollisionEnter is called. Here is the code I use for that:
void OnCollisionEnter(Collision collision)
{
foreach (ContactPoint c in collision.contacts)
{
if ((c.thisCollider.renderer.material.color == c.otherCollider.renderer.material.color)
&& (c.thisCollider.renderer.material.color != Color.gray)
&& (c.otherCollider.renderer.material.color != Color.gray))
{
ScorePoint = true;
c.thisCollider.renderer.material.color = Color.gray;
c.otherCollider.renderer.material.color = Color.gray;
}
}
(the blocks are coloured gray after colliding with the same colour)
The actual problem I have is illustrated below. In the first frame the gameobjects collide and no matching colours are detected. The collision between the gameobjects holds until two matching coloured blocks are touching, but of course OnCollisionEnter will not be called. I have tried playing with the OnCollisionStay event but I haven’t managed to do so without raising huge performance issues on mobile devices.
If anyone has any advice it would be greatly appreciated. Perhaps a solution which works only in this special case where all the individual blocks are cubes?